301 redirect like *.$domain.$tld -> www.$domain.$tld

Discuss practical ways rearrange URLs using mod_rewrite.

301 redirect like *.$domain.$tld -> www.$domain.$tld

Postby josh675432 » Thu Aug 28, 2008 10:16 am


I have multiple sites being served up from the same apache instance. I'm trying to create the mod_rewrite rules to do something like this:

*.$domain.$tld -> www.$domain.$tld

foo.com -> www.foo.com
dfsa.foo.com -> www.foo.com
foo.co.uk -> www.foo.co.uk
blah.com -> www.blah.com
fdsa.blah.co.uk -> www.blah.co.uk

Any help would be greatly appreciated

Posts: 2
Joined: Thu Aug 28, 2008 10:08 am

Postby richardk » Fri Aug 29, 2008 8:40 am

You will need to list the second level domains (.co.uk).

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(.+\.)?([^\.]+\.(co\.uk|gov\.uk))$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(.+\.)?([^\.]+\.[^\.]+)$ [NC]
RewriteCond %{HTTP_HOST}:www.%2 !^([^:]+):\1$
RewriteRule ^(.*)$ http://www.%2/$1 [R=301,L]
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

thanks very much

Postby josh675432 » Fri Aug 29, 2008 1:21 pm

Thanks very much!

I ended up having to change the last line to:

Code: Select all
RewriteRule ^(.*)$ http://www.%2$1 [R=301,L]

otherwise I ended up w/ a double slash at the end.
Posts: 2
Joined: Thu Aug 28, 2008 10:08 am

Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 3 guests