views:

27

answers:

1

I'm trying to make a general rewrite rule to redirect all domain.com requests to www.domain.com.

RewriteCond %{HTTP_HOST} ^([0-9a-z-]+)\.([0-9a-z-]+])$ [NC]
RewriteRule ^(.*)$ http://www.{HTTP_HOST}/$1 [R=301,L]

The problem is that this rewrite rule doesn't match anything. How can I change it? Thanks

A: 

I guess the problem is that there’s an additional ] in your RewriteCond’s pattern and that there is a % missing when referencing HTTP_HOST in RewriteRule’s substitution. So try this:

RewriteCond %{HTTP_HOST} ^([0-9a-z-]+)\.([0-9a-z-]+)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

And to cover Cags’ concerns a litte bit, you can also try this rule:

RewriteCond %{HTTP_HOST} ^[^./]+\.[^./]+$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([^./]+)\.[^./]+\.[^./]+$ [NC]
RewriteCond %1 !=www [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Gumbo
thanks, this is the solution
Derk