DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 5.17 Redirecting All Requests to a Single Host

Problem

You want all requests made of your system to be redirected to a specific host.

Solution

Put this in your httpd.conf:

RewriteCond "%{HTTP_HOST}"   "!^www.example.com$"       [NC,OR]
RewriteCond "%{SERVER_NAME}" "!^www.example.com$"       [NC]
RewriteRule "(.*)"           "http://www.example.com$1" [R]

Discussion

Any request handled by your server within the scope of the directives in the Solution (which aren't directed to the www.example.com host) is redirected there.

The two different RewriteCond directives are used to catch all requests made by some host other than www.example.com, regardless of the redirection method.

The NC (No Case) flag makes the regular expression case-insensitive. That is, it makes it match regardless of whether letters are upper- or lowercase.

The OR flag is a logical "or," allowing the two conditions to be strung together so that either one being true is a sufficient condition for the rule to be applied.

Finally, the R flag causes an actual Redirect to be issued, so that the browser will make another request for the generated URL.

See Also

    [ Team LiB ] Previous Section Next Section