Tuesday, December 9, 2014

How to Redirect my website from non www to www with htaccess?

How to Redirect non-www to www in .htaccess?
Redirecting non-www to www with .htaccess. How?
How to force www or non-www in htaccess?
How to redirect non-www URLs to www?
non www to www htaccess wordpress
htaccess non www to www 301
htaccess www to non www https

This is a more generic solution, because it can be used with any domain name without having to specify the specific domain name in each .htaccess:

# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

The contrary is also possible (www to non-www):

# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]


Another Way to Redirect

I think it may just be that your existing rule is too strict, and is not getting triggered in your sub-directory because of this. Something like this should work site-wide:

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^example\.com$

RewriteRule ^(.*) http://www.example.com/$1 [R=301]