✕
Share it

Contact me
iPhone / iPad

Tools

Hindi Proof Reader Top Tech Blogs in India QR Code Generator
Make Money Online Earn from Blogging More Tools MS-Excel MS-Word AdSense About Lalit Contact us
TechWelkin Site Logo

TechWelkin

Tips and Tutorials on Computers, Mobiles and Internet

Last updated: 22 September 2017

Redirect www to non www URL Using HTACCESS

January 6, 2015 By Lalit Kumar 15 Comments

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:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.olddomainname.com$ [OR]
RewriteCond %{HTTP_HOST} ^olddomainname.com$
RewriteRule ^(.*)$ http://www.newdomainname.com/$1 [R=301,L]
Redirect www to non-www URLs and vice versa using HTACCESS.

Redirect www to non-www URLs and vice versa using HTACCESS.

Redirect www to non-www Domain

Add the following code in .htaccess file to permanently redirect all the www addresses to their non www counterparts:

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

Replace ‘example.com’ with your domain name. If you want a more general sort of solution, it was offered by Ben on StackOverflow:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

As you can see, you don’t need to change anything. Just paste the above code in .htaccess and all your www URLs will be permanently redirected to the corresponding non-www URLs.

Redirect non-www to www URL

The following code will redirect all the non-www addresses of a domain to their www counterparts:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [nocase]
RewriteRule ^(.*) http://www.example.com/$1 [last,redirect=301]
www to non-www URL on a shared host server (server with multiple domains)

I am taking up this specific issue because many webmasters feel confused when the following happens:

http://www.example.com/page.html
redirects to

This situation arises on a server that hosts multiple domains. Mostly such servers are shared hosts or reseller type of hosting arrangements. Use the following code:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?example\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
How to redirect an old folder to a new folder name?

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!

Related Articles for More Information:

  1. Check and Test .htaccess File for Correctness
  2. MediaWiki URLs: Short, Clean and Beautiful
  3. How to Remove WordPress Mobile Pack (WMP)
  4. Check if a Web Page URL has Redirect 301, 302 or 307
  5. Perl: How to List All Files in a Directory
  6. URL Change: Get Facebook Like Count Back
Last updated: 22 September 2017 | Published on: 6 January 2015 | Authored and Edited by: Lalit Kumar and Team | Filed as: Home » Tips for Success in Blogging » Redirect www to non www URL Using HTACCESS

Filed Under: Tips for Success in Blogging

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).

Comments

  1. Michael says

    February 5, 2017 at 4:39 pm

    Great article! I found good and working solution to do 301 from www to non-www thank to you! Thank you.

    Reply
  2. Aswani says

    January 25, 2017 at 10:53 am

    Thanks for this info. I have been facing issues with redirection. This will surely help me out in solving redirection issues on my server.

    Reply
  3. Himanshu Rawat says

    January 7, 2017 at 10:56 am

    Thanks a lot. with htaccess redirection is much faster. Thanks again

    Reply
  4. Apurva says

    November 10, 2016 at 11:20 am

    Hi sir i want to know which domain format is important? with www. or without it? can any one please let me know.

    Reply
    • Lalit Kumar says

      November 12, 2016 at 2:21 pm

      Both formats are equally ok. There is no practical difference.

      Reply
  5. Jaswinder says

    March 11, 2016 at 1:40 am

    Very valuable article.

    Is it important to redirect from one to another or vice versa-means www to non www or from non www to with www url?

    Reply
    • Lalit Kumar says

      March 11, 2016 at 11:01 am

      That’s a matter of personal liking.

      Reply
  6. Fahad Latif says

    December 5, 2015 at 10:50 pm

    You are the man. I really like your work and guidance to resolve such critical issue.

    Thanks again.

    Reply
  7. Jaydip Parikh says

    November 17, 2015 at 2:58 pm

    Hello Lalit,

    Thanks for detailed explaining things for www vs non-www and also providing htaccess code for the same.

    Reply
  8. Praveen says

    August 24, 2015 at 6:08 pm

    Thanks for sharing nice peace of code built for 301 redirect using the .htaccess file, i was looking for the code.

    Reply
    • Lalit Kumar says

      August 25, 2015 at 4:20 am

      You’re welcome Praveen! All the best for your endeavors.

      Reply
  9. Arsh says

    July 23, 2015 at 3:15 am

    Thanks Buddy !

    Reply
    • Lalit Kumar says

      July 23, 2015 at 4:33 am

      Thanks Arsh, I am glad you were able to solve www redirect problem in your website. Stay connected with TechWelkin!

      Reply
  10. Srp says

    June 20, 2015 at 10:10 am

    How to redirect this for asp.net based website.

    Reply
    • Lalit Kumar says

      June 22, 2015 at 6:09 am

      In case of Windows hosting, most probably you’re using IIS Server (which runs ASP.net code). The HTACCESS does not work on IIS Server. Instead of htaccess, you should use web.config file.

      Reply

Leave a Reply Cancel reply

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

Recent Stories

  • Check Ticket Confirmation PNR Status on WhatsApp
  • Easily Capture Screen and Do Recording on iPhone / iPad
  • Consistent Video Upload is Important for YouTube Channel
  • Conditions for YouTube Channel Monetization
  • How to Get Your First 100 and 1000 Subscribers on YouTube
  • What is the Average Audience Retention for YouTube Channels?
  • Remove Activate Windows Watermark from Windows 10
  • Disadvantages of Using Google Custom Search Engine

Tools

  • Hindi Proof Reader
  • Top Tech Blogs in India
  • QR Code Generator
  • Random Word Generator
  • Strong Password Generator
  • Trace Mobile Numbers

Connect with us

  • Facebook
  • Google+
  • Twitter
  • LinkedIn
  • Pinterest
  • RSS Feed

© 2007-2019 TechWelkin.
The content is copyrighted to Lalit Kumar and may not be reproduced on other websites.
TechWelkin displays Google AdSense ads. See how Google uses data when you're on TechWelkin | Privacy Policy