How to Redirect WWW to Non-WWW and Non-WWW to WWW with htaccess

I’m writing this tutorial about How to Redirect WWW Urls to Non-WWW and Non-WWW Urls to WWW with htaccess. Most of the Webmasters may have seen a Warning in Google Webmasters Tools about Duplicate Meta Titles or Duplicate Meta Descriptions. This tutorial will help you to get rid of those warnings. These issues mostly occurs because Multiple Urls in your Website are pointing to the same page which creates identical pages in Google Index or Yahoo Index. This is because search engines treats Non-WWW Urls and WWW Urls as two separate pages/domains like http://example.com and http://www.example.com.

Redirect Non-WWW to WWW and WWW to Non-WWW Urls with htaccess

Both the WWW Version (i.e http://www.example.com) and Non-WWW Version (i.e http://example.com) of the URL will be indexed as two different pages in Search Engines. Perhaps this Duplication leads to a Penalty or complete removal of your Website from Search Engines.

To avoid Page Duplication, you can do the following things:

  • Select either www version of urls or a non-www version.
  • Change all urls to selected version of URL.
  • Redirect all urls to selected version of URL (For Removing Duplicate Pages Completely).

You should be very careful in selecting which version of Urls you are going to use. Do a proper research about it. See your Traffic Stats and then analyze that which version of Urls is getting better traffic on your site, then choose that version and force your users to use that version as well. For achieving this, you should Redirect Your Users forcefully to the New Urls.

After changing your Url Structure to either WWW or Non-WWW, you should apply a Permanent Redirect (also called 301 Redirect) to those pages which were creating duplicates in Search Engine Index. Means if you selected WWW Version, then you should redirect all non-WWW Urls to WWW and vice versa. Here is the code for doing this with APACHE HTACCESS:

Redirecting Non-WWW to WWW URLS with HTACCESS:


Above code will Redirect every visitor/bot from http://yoursite.com to http://www.yoursite.com. This is a permanent redirect for telling the bots that you should forget the non-www url and only index the new url. This will increase your website’s score in front of bots.

Here is the code for Redirecting WWW Urls to Non-WWW.

WWW to Non-WWW Url Redirect using HTACCESS:


In above code, you just need to replace Yoursite with your Site’s Name. Also change the TLD to your’s.

If you do not have .HTACCESS available, you can use HTTPD.CONF file which includes all APACHE Configurations in it. See this code:

WWW to Non-WWW Url Redirect for Apache HTTPD.conf

Add the following code into your HTTPD.conf file located in your server.

The above code will Redirect only urls with WWW. And now similar code is for Redirecting Non-WWW urls to WWW Urls.

Non-WWW Urls to WWW Urls Redirect for Apache HTTPD.conf

Paste the above code into HTTPD.conf file if you are planning to redirect non-www to www urls. Congrats, your URL Canonical Issues will be solved now.

Hope you like it.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

scroll to top