non-www domain to www domain

Topics: Developer Forum, User Forum
Mar 11, 2009 at 4:07 PM
I have tried several variations of this rule to force a www to the beginning of the domain name.

RewriteCond %{HTTP_HOST} ^$
RewriteRule ^/(.*)$$1 [R]

I cannot for the life of me get it to work, I believe this should work and should work fine.
Can someone verify that this is indeed the correct form for this rule.

Jun 22, 2009 at 7:05 PM

The following code worked for me, although for some reason I had to specify R=302 (which should be the default value?) in order to get it to work...

RewriteCond %{HTTP_HOST} ^$
RedirectRule ^/(.*)$$1 [R=302]

Jun 25, 2009 at 6:40 PM

The reason the original did not work is that it used RewriteRule when RedirectRule was needed.

If you look in the log file you will see an error message indicating the problem.

Also, the [R] is an old syntax when RewriteRule used to have two meanings : Rewrite or Redirect.   Now that there is the RedirectRule directive, the bare [R] is not necessary and it may even make the parser choke (I don't recall; check the log file).  You can specify [R=301] or [R=302] for "Moved Permanently" or "Moved Temporarily" respectively.  If you don't specify either, it is the same as [R=302].