Question

I previously got some great help here with a tad bit complicated .htaccess file.

It is a multi-site/domain file and uses %{HTTP_HOST} to rewrite/301 the non-wwww to the www.widgets.com address.

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

The problem is that RegexFu only looks for the NOT www. and then adds the www.

This could cause some duplicate content issues if someone links to say, mail.widgets.com as this is redirected to www.mail.widgets.com.

To summarize:

I need:

mail.widgets.com -> www.widgets.com
dogs.widgets.com -> www.widgets.com
www.mail.widgets.com -> www.widgets.com
www.dogs.widgets.com -> www.widgets.com

etc.

I have tried a variety of permutations liek this with no luck:

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

No dice. I either end up with a loop or some other problem.

I need simply anything that isn't:

www.   (anything but period)     .com - > www.    (last string before .com)         .com

and of coarse the standard:

(anything but period).com -> www. (anything but period).com

If someone could lend me a hand I would greatly appreciate it.

Was it helpful?

Solution

Try:

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

The %1 backreferences the grouping that matches ([^.]+) in the previous rewrite condition. It ignores any subdomain that's before the domain name, if there is one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top