Thursday, March 10, 2016

Redirect non-www web address to www in .htaccess

How to redirect non-www web address to www in .htaccess

Bellow are the information ot put in .htaccess but sometimes no information in the file,

RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com$1 [R=301,L]

For some reason the web is not respond or change redirect automatically. The solution is bellow

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

This will redirect any requests to http://my-domain.com to http://www.my-domain.com

If you want to do the opposite, put these code bellow

RewriteEngine On
RewriteCond %{HTTP_HOST} !^my-domain\.com$ [NC]
RewriteRule ^(.*)$ http://my-domain.com/$1 [R=301,L]

Remember to change my-domain.com with your domain name

No comments: