ManyCodes.com – codes & scripts Get free programming codes and tutorials!

6Aug/090

How to 301 Redirect Non-WWW to WWW URLs

Search engines consider http://manycodes.com and http://www.manycodes.com different websites. As a result, if your website has been linked to from other websites using a mix of the two URLs you are effectively splitting the potential benefit of valuable link popularity.

The Solution
Using a 301 redirect on the “non-www” version of the URL, which is essentially a “permanent” redirect in server talk, you can effectively consolidate all of your link popularity to a single URL. This consolidation will serve to increase your website’s chances of obtaining and maintaining top rankings.

How To Enable The 301 Redirect

You need to instruct the server you are hosting your website on to redirect the traffic seamlessly. To do this you need to first establish what type of server your website is hosted on. There are two main types of servers that are in use: Microsoft and Apache (Linux/Unix). Once you have established the server type you need to follow their related instructions below. Please note this is best left to a person with some technical knowledge.


A) Installing the Non-WWW 301 Redirect on a Apache Server

  1. Ensure that your hosting provider has the Apache Rewrite Module turned on; this is a definite requirement for this fix to work. In most cases your hosting provider will not have a problem enabling the module if it isn’t already enabled (it seems to be on in most circumstances).If you have access to the httpd.conf file on your server then you can actually enable the module yourself. Open the httpd.conf file, back it up somewhere in case of error (you cannot be too safe) and then uncomment the following line:LoadModule rewrite_module modules/mod_rewrite.soOnce the line is uncommented, save the file and restart the server then proceed with the following steps.
  2. Download the .htaccess file from your website’s root web folder to a folder on your machine where you can edit it.
  3. Make a copy of the .htaccess file and save it somewhere else for safe keeping in case you need to revert to the old version – do not edit this version.
  4. Open the original .htaccess you downloaded in Notepad
  5. Add the following code (in red) into the .htaccess file. Be sure to substitute “example.com” with your own website information but touch nothing else.RewriteEngine On
    RewriteCond %{HTTP_HOST} ^example.com
    RewriteRule (.*) http://www.example.com/$1 [R=301,L]
  6. Upload the file back to your website in the exact same place you downloaded it.
  7. Once the upload is complete open a browser and try visiting the “non-www” version of your website address. If the .htaccess is working as it should you will be redirected immediately to the proper “www” version of your website. This will be reflected in your browser’s address bar.
  8. To be 100% certain this has worked appropriately I suggest visiting this URL and typing in your “non-www” website address into the HTTP Server Header Checker. This tool will show you whether the 301 redirect is being provided by the server. This is what a search engine will see when it visits the site. The result should look like the following areas in red:
    #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
  9. If the redirect worked you are done! Congratulations.If this has not worked you need to restore the backup you made of the .htaccess file to your website. After the backup is restored go back and review the revised .htaccess on your computer, compare the information to the instructions above and make sure there were no mistakes. If no mistakes are found your server may require custom programming which is beyond the scope of this FAQ; please contact your hosting provider for more information.

B) Installing the Non-WWW 301 Redirect on a Microsoft IIS Server

Microsoft servers do not have a .htaccess file to alter so we suggest that you contact your hosting provider and request they make this change for you. If they are baffled or need further instructions on how to make the changes you can refer them to the tutorials referenced below:

  1. Using Internet Services Manager create a new IP-based website using the http://example.com URL or alternatively you can avoid using a unique IP by using the host header (virtual website) of www.example.com.
  2. Now verify the server headers for each website using the Server Header Checker. The server response should be 200 OK for both addresses.
  3. Now add your domain-revised version of the following ASP code to the default home page for http://example.com:< %@ Language=VBScript %>
    < %
    Response.Status=”301 Moved Permanently”
    Response.AddHeader “Location”, http://www.example.com
    %>
    Note: do not change the spacing or line placement within the above code; place it as is.
  4. Once the default page is online first visit your website via http://example.com to ensure the redirect is working. Next check the server headers for http://example.com and make sure you see the following code within #1: “HTTP Status Code: HTTP/1.1 301 Moved Permanently”. That code will confirm the 301 redirect is being properly communicated.



Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Webnews
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • Ask
  • Facebook
  • Technorati
  • YahooBuzz