Reputation: 33
I redirected non-www request to www through .htaccess Rewrite Rule.
RewriteCond %{HTTP_HOST} !^www
RewriteRule (.*) www.%{HTTP_HOST}/$1 [L,R=301]
But now I am having problems with subdomains. When I am accessing touch.111.com
then the above rule redirects to touch.www.111.com
(which is not accessible), and the website breaks on touch devices.
Please advise me on how to fix the above problem.
Upvotes: 3
Views: 8781
Reputation: 11
# For sites running on a port other than 80
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^/(.*) http://www.example.com:%{SERVER_PORT}/$1 [L,R]
# And for a site running on port 80
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.example.com/$1 [L,R]
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
Upvotes: 0
Reputation: 31
This is a generic solution that can work for any domain name:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
see http://www.htaccessredirected.com/force-redirecting-non-www-to-www-htaccess.htm
Upvotes: 1
Reputation: 3432
You must be specific if you want to redirect only domain.com
to www.domain.com
and retain sub-domains (such as touch.domain.com
) :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(.*) http://www.domain.com/$1 [L,R=301]
Upvotes: 4