views:

359

answers:

2

If I go to http://www.example.com I want it to stay there, which is working fine.
If I go to http://bar.example.com it redirects to http://www..com, which is wrong
I want it to go to http://www.example.com given the backreference in the RewriteCond

RewriteEngine On

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

Ubuntu 8.04
Package: apache2-mpm-prefork
Architecture: i386
Version: 2.2.8-1

A: 

Your condition does not match bar.mysite.com:

RewriteCond %{HTTP_HOST}   !^www.(mysite).com [NC]

You need to change it to match in order to get the backreference working:

RewriteCond %{HTTP_HOST}   !^[^\.]+\.(mysite)\.com [NC]
Andrew
+1  A: 

Negated patterns have no match and thus you cannot reference a group of that non-existing match.

But try this rule instead:

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*) http://www.example.com/$1 [L,R=301]
Gumbo
Ahh negated pattern. It all makes sense now. Thanks!
er1234