Redirecting non-www to www with .htaccess

Home / PHP / Redirecting non-www to www with .htaccess

If your website can be access both way with and without www. So may search engines consider same content with two different URLs with and without www. So it might be a case of Duplicate Content as well as Google Canonical problems. So you must stick your domain name accessible either with www or without www.

Before writing this rules in .htaccess , make sure that your mod_rewrite is enabled(On). Most probably mod_rewrite is enabled in Linux server but for windows server you need to contact hosting people to make mod_rewrite enabled. You can check this by looking in phpinfo().

How to redirect www to non-www and non-www to www URL using .htaccess redirect

Redirect www to non-www:

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

Redirect non-www to www:

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

If you already have a file named .htaccess on your website you can add to it. If not, create one (yes, include the period at the beginning). Add either of the following rules and save. Replace justwebdevelopment.com with your actual domain name.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *