How to Redirect non www to www in .htaccess

Published On December 26, 2013 | By Felicia Nicole | WordPress

Most of the search engines tend to consider websites bearing the prefix www different than those without www. For instance, the search engines would consider ” http://example.com ” and ” www.example.com ” as two different websites. As a result of this, if the website is linked to from different websites using both the URLs, you end up splitting the traffic value and benefit.

A 301 redirect is a tool which allows you to permanently redirect the traffic to the non-www link. Most website owners realize the importance of the 301 redirect tool since it allows the sites owners to reap the benefits of their site popularity to a particular URL. This focused traffic and popularity is necessary in today’s internet dominated marketing scenario, as it enhances your search engine rankings.

301 Redirect can be enabled by giving necessary directions to the server which hosts your website to redirect the traffic to your website link. For this, you must identify the kind of server website is hosted on. The two main choices of servers are: Microsoft or Apache (Linux). Once you have identified this, you can follow the guidelines mentioned below. It is also advisable to take technical assistance, to avoid hassles at any step.

Installing 301 Redirect on Apache Server:

At the outset, you need to ensure that Apache Rewrite Module is turned on. Ask your hosting provider to do the needful. Else if you have direct access to the httpd.conf file on the server you can module it yourself easily. It is suggested to make a back-up copy of httpd.conf file to use in case you mess up while updating it. Uncomment LoadModule rewrite_module modules/mod_rewrite.so line.

Once you have enabled the same, download .htaccess file from the root web folder of your website and save a copy of the same in a separate folder. As a best practice, make a back-up copy. Then open the downloaded version of the .htaccess file in Notepad and add the following code –

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

Code source: http://www.stepforth.com/resources/web-marketing-knowledgebase/non-www-redirect/#.UqmYoHUW2

Substitute your domain name in place of example.com in the above code and then upload the same to the root web folder. Now put the non-www version of your website in the address bar of the browser and check whether you are redirected to the www version or not. If yes, then it automatically implies that the 301 Redirect process has worked. To be double sure use HTTP Server Header Checker. This tool gives you the confirmation that 301 redirect is being offered by the server. The following code will appear when it visit the website.

#1 Server Response: http://example.com

HTTP Status Code: HTTP/1.1 301 Moved Permanently

Date: Wed, 14 Mar 2007 22:49:28 GMT

Server: Apache/1.3.27 (Unix) PHP/4.4.1 FrontPage/5.0.2.2510 mod_ssl/2.8.14 OpenSSL/0.9.6b

Location: http://www.example.com/

Connection: close

Content-Type: text/html; charset=iso-8859-1

Redirect Target: http://www.example.com/#2 Server Response: http://www.example.com/

HTTP Status Code: HTTP/1.1 200 OK

Date: Wed, 14 Mar 2007 22:49:28 GMT

Server: Apache/1.3.27 (Unix) PHP/4.4.1 FrontPage/5.0.2.2510 mod_ssl/2.8.14 OpenSSL/0.9.6b

Connection: close

Content-Type: text/html

Code source: http://www.stepforth.com/resources/web-marketing-knowledgebase/non-www-redirect/#.UqmYoHUW2

In case not, then you need to re-do the entire process and ensure that no mistakes are made. If it still does not work, then get in touch with your server company and try making adjustments to the settings.

Installing 301 Redirect on Microsoft Server

Since Microsoft servers do not have the .htaccess files, you may not be able to carry out the editing process, as described above. Hence, in order to enable 301 Redirect for a website hosted on a Microsoft server, you must contact the concerned server or web hosting company. The concerned technician at the web hosting company would create a new IP-based non-www website using URL. They would thereafter conduct a check on the server headers of the websites and add the following ASP code to the default home page for the new IP-based website:

< %@ Language=VBScript %>
< %
Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”, http://www.xyzname.com
%>

Code source: http://www.stepforth.com/resources/web-marketing-knowledgebase/non-www-redirect/#.UqmYoHUW2

Substitute your domain name in place of example.com in the above code. Now first visit your own website via the new IP-based URL to verify that the 301 Redirect is working and then check the server headers for the same. If you see the code –  #1: “HTTP Status Code: HTTP/1.1 301 Moved Permanently”, that means the process has been duly completed and 301 Redirect is active.


Like this Article? Share it!

About The Author

Leave a Reply