Redirect www to non www URL Using HTACCESS

January 6, 2015

Learn to redirect www to non-www URLs or non www to www URLs using .htaccess file. You can do 301 permanent redirect on shared server with multiple domains also. If you have a self hosted WordPress website, you it would be useful for you.

The Internet is full of resources and all these resources are uniquely identified by Uniform Resource Locators (URLs). Importance of the uniqueness of URLs can not be overstated and need not be explained. It is very simple to understand! Most web servers and browsers consider www and non www URLs as same. For example, theoretically:

http://www.example.com is same as

But some web servers and search engines (like Google) may consider these URLs to be different. Both of the URLs point to the same resource. So, we have two URLs to access one resource. Such a situation causes content duplication and content duplication is a cardinal sin in the eyes of search engines. They put a heavy penalty on websites hosting duplicate content.

Internet is a dynamic system. Here resources change and so do the URLs. Therefore, redirection is a common phenomenon on the Internet. Webmasters deploy a few methods to redirect www URL to non www URLs (or the other way round, i.e. redirect non-www URL to www URL). the easiest method to achieve this is to use .htaccess file wherein you can permanently redirect (301 redirect) one form of the URL to another form. Let’s learn how to carry out such redirection. But before that, let’s see what all you would need:

Make a decision

First of all, you would need to decide which format you prefer. You want to keep your website’s URLs in www format or non www format. Whichever you choose, stick to one format. Do not mix www and non www URL formats. Make sure mod_rewrite module of Apache is available.

Modifying .htaccess will require you to have access on your server. If you’re writing a blog on a free platform (like WordPress.com), you would not be able to access or change .htaccess file. But in such a case you would not any way need to make such changes as the blogging platform does take care of such small issues.

mod_rewrite module of Apache server

Apache server uses modules for various functionalists. mod_rewrite is a module that enables redirection and URL rewriting. If this module is installed and available on Apache server, only then your redirection will work.

If mod_rewrite is available, it will take your redirection commands from .htaccess file.

Redirect old domain to new domain

If you have changed the domain name of your website, it is highly recommended that you redirect all the old website pages to the new address. To do this, add the following in .htaccess file:

As I have mentioned, you should try to avoid making changes in the URLs but sometimes it is necessary and unavoidable. In case you have to rename a folder, you should use the following code to redirect URLs with old folder to the new folder.

RewriteEngine on
RewriteRule ^/?old([a-z/.]*)$ /new$1 [R=301,L]

Do not forget to change ‘old’ and ‘new’ with the old and new names of your folder.

I hope this information was useful for you. Make sure redirection is done properly. A badly configured .htaccess can break your server. It is an extremely important file. Correct URL redirection will benefit not only your website or blog users; but also it will have a good impact on your website’s search engine optimization (SEO).

Should you have any, please feel free to ask any questions. Thank you for using TechWelkin!

Lalit Kumar is the Principal Author and Founder of TechWelkin. He is a web explorer and he enjoys finding useful information on the Internet. He loves to put things together to create a bigger solution. Lalit is passionate about technology, languages and literature. You can contact Lalit via email (techwelkin [at] gmail [dot] com) or Facebook (facebook.com/techwelkin).