Htaccess – www to non-www domain name

Good SEO practice suggests that one domain be chosen as Google, Bing and other search engines see www.mysite.co.za and mysite.co.za as 2 separate domains. To redirect all requests to the non www version of your domain, use the following code in your htaccess file


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


#change non-www to www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.co.za [NC]
RewriteRule ^(.*)$ http://www.mysite.co.za/$1 [L,R=301,NC]

The NC indicates the URL us not case sensitive. It does not matter if uppercase or lowercase letters are used is the URL. The L means ‘last’. When it runs across the L, the process should stop where it is and not apply any more rewrite rules so you would want to put this at the bottom of your htaccess file.

Need a website, consultation or customisation? Let’s speak