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

Hello!

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

so:
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
....etc

Any help would be greatly appreciated

:D
josh675432
 
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).

Try
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]
richardk
 
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.
josh675432
 
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 43 guests

cron