Forum Moderators: phranque

Message Too Old, No Replies

simple domain rewrite

         

minn3h

4:54 pm on Jul 14, 2009 (gmt 0)

10+ Year Member



I have several domains all pointing to the same files on the same web server. I'm trying to get all of those domains to rewrite to one particular domain. As far as I can tell something simple like this would work except that it would cause an infinite loop:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Is there a way to write a condition that will do that rule unless the domain is already correct, i.e. www.example.com? Or is there a better way to go about this?

jdMorgan

6:00 pm on Jul 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes:

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

This redirects any non-blank, non-canonical hostname request to the canonical domain.

The provision for blank hostnames is needed to prevent an infinite loop on servers that can accept HTTP/1.0 requests, on the odd chance that you might get one (or a spoofed one).

Jim

minn3h

6:33 pm on Jul 14, 2009 (gmt 0)

10+ Year Member



Perfect!