Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite and 301's for a dedicated server

If not example.com then 301

         

trillianjedi

12:16 pm on Dec 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On a dedicated server I've just noticed that I have a bunch of rewrite rules to cater for additional domains, eg:-

example.co.uk
example.org
example.net
someothername.com
(no www)example.com

.... all 301 to:-

www.example.com

At the moment, these are all dealt with under separate rules, like:-

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

Is there any way of encompassing this under one rule? My thought is basically anything that does not begin with "www.example.com" should be 301'd there. That would basically mean that any domain pointing to this server would get 301'd to a single URI.

Based on that idea I tried the following but it didn't work:-

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

Is that simply an error in my syntax, or is this not possible?

Thanks!

TJ

jdMorgan

1:58 pm on Dec 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you missing the space between "}" and "!"?

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

PHPbb nukes that space unless you take steps to avoid it.

Jim

trillianjedi

2:07 pm on Dec 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ack!

It was that simple. Thanks Jim - that's another 20 lines of junk removed from my .htaccess file :)

TJ

jdMorgan

3:38 pm on Dec 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I like to add a couple of things to this type of negative-match HTTP_HOST redirect. First is a test for HTTP/1.0 requests. These requests do not send the "Host:" HTTP request header, and as such, can cause an infinite redirect loop if you don't prevent it. This is only a concern if your host is reachable by a unique IP addresss, as otherwise, requests with no Host header can never reach your server.

The second feature I like to add is a back door for accessing the site if the DNS goes down or the domain expires (gasp!). Here I show an exception to the redirect if the host is requested by its IP address from my home or office PC's IP address:


# Redirect everything to www.example.com
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}<->%{REMOTE_ADDR} !^your\.host\.ip\.address(:80)?<->(your\.home\.IP\.address¦your\.office\.IP.address)$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

The IP addresses are shown symbolically, of course, and will actually be numerical, as in 120\.34\.56\.78
These can be single IP addresses or IP address ranges, as desired.

Another common exception (not shown here) is to bypass the redirect for control panel or stats directories that are set up by the host to always use IP-address access. Again, this only applies if your server has a unique IP address.

The "<->" character sequence has no special meaning. It is simply a unique string used to delineate the boundary between the two variable values, in order to prevent pattern-matching ambiguity.

Replace the broken pipe "¦" character above with a solid pipe character before use; Posting on this forum modifies the pipe character.

Jim

trillianjedi

4:55 pm on Dec 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're the man Jim!

Great config - thanks.

One question - if I put this into httpd.conf, rather than .htaccess, the "/$1" seems to get ignored.

In other words:-

thewrongdomain.com/foo.html

Will get 301'd to:-

www.example.com/foo.html

If this rewrite code is in .htaccess, but it 404's if it's in httpd.conf.

Is there any specific reason why that would be?

Thanks...

jdMorgan

5:17 pm on Dec 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



.htaccess is a per-directory context. What that means is that the path ("/" in this case) to the .htaccess file's directory is stripped form the URL-path that RewriteRule examines. So you cannot simply move code from .htaccess to httpd.conf --or the reverse-- without adjusting it for its new location, which in this case is now above Web root.

Change the rule to either one of the following:


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

Jim

trillianjedi

7:23 pm on Dec 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Jim. I still can't get that to work right, but at least I know why.

I'll try and tweak it this weekend and I'll report back when I've got it working and describe what the problem was.

jdMorgan

9:33 pm on Dec 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please note that I said "change the rule," and I left out the RewriteCond stuff, some of which is required.

Jim