This will change domain.com
to www.domain.com
:
# Force the "www."
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
How do I replace the "domain" part so that this works on any domain?
This will change domain.com
to www.domain.com
:
# Force the "www."
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
How do I replace the "domain" part so that this works on any domain?
I would use this rule:
The first condition checks whether the Host value is not empty (in case of HTTP/1.0); the second checks whether the the Host value does not begin with
www.
; the third checks for HTTPS (%{HTTPS}
is eitheron
oroff
, so%{HTTPS}s
is eitherons
oroffs
and in case ofons
thes
is matched). The substitution part ofRewriteRule
then just merges the information parts to a full URL.If you want to redirect all non-www requests to your site to the www version, all you need to do is add the following code to your .htaccess file: