0

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.

Samantha P
  • 543
  • 4
  • 12

1 Answers1

2

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.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Closer. I thought it was going to work :) www.person.widgets.com doesn't rewrite to www.widgets.com :( I would guess that is because of the !^www\. condition. person.widgets.com DOES however rewrite to www.widgets.com now.... – Samantha P Dec 11 '12 at 12:52
  • @SamanthaP Added an additional OR'ed condition in case it starts with www but has more than one 3rd level domain name – Jon Lin Dec 11 '12 at 12:58
  • Odd. It seems like with this as the first line it works: RewriteCond %{HTTP_HOST} !^www\.([^.]+)\.com$ [NC] – Samantha P Dec 11 '12 at 13:00
  • Wow, you can use OR boolean in htacces?!? *eyes twinkle with possibilities* – Samantha P Dec 11 '12 at 13:00
  • Okay, so the () is the capturing group..... the same regex without the () still matches but doesn't capture, right? – Samantha P Dec 11 '12 at 13:01
  • @SamanthaP Hmm, yeah that works too. It would replace the first 2 conditions, but you need the 3rd condition in order to match the 2nd level domain. – Jon Lin Dec 11 '12 at 13:01
  • @SamanthaP Without the `()`'s you wouldn't be able to backreference them in the following directives. – Jon Lin Dec 11 '12 at 13:02
  • Yeah, accepting your answer - it was worth it just for the lesson, Thanks Mr. Lin :) – Samantha P Dec 11 '12 at 13:03
  • So do % always reference groups captured in a condition and $ in the rule itself? – Samantha P Dec 11 '12 at 13:04