Wise Web Hosting

An Australian Based
Web Hosting Business

Portal Home > Knowledgebase > Getting Started > Rediect from www to non-www and vice versa using .htaccess

Rediect from www to non-www and vice versa using .htaccess

After building your new website, set up a web hosting package and updated nameservers, something which is commonly overlooked is the www redirect.

It's advisable to redirect your domain to either include or exclude the www prefix.

This is easy with Apache, all you have to do is update your .htaccess file found in the web root directory of your website.

Here's an example below. In this case we will redirect the www sub-domain to the primary domain.

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]

RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]

All this does is checks if a www exists, if it does, remove the www and redirect the user to the primary domain.

If you want to do the opposite and redirect users to the www sub-domain, place these 2 lines in your .htaccess file:

RewriteCond %{HTTP_HOST} !^www\. [NC]

RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read

Powered by WHMCompleteSolution