Htaccess redirect www to non www. http & https

This article will provide you htaccess rules to redirect a www website to non www.  I prefer to make rule regardless of using name of specific domain so no-where in this article I have used domain name. The general sort of  solution to make www to non www apache htaccess redirect with http protocol (Good if you are not using secure http connection {https} or SSL).

htaccess redirect www to non www. http & https

The code block below is extension of above first one and is helpful if you are using both http and https and again, you don’t need to specify domain name.

But wait! We can have more simpler solution. Here is the code for removing the www from your hosts, regardless of http or https protocol:

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 regardless of http or https.

You Might Interested In

  • Ali Raza

    Thank you very much for this post. It help me alot to redirect both www and non www and https www to https non www but i see that in code 3 there is no RewriteEngine on line so please tell me is it necessary to add or optional because without it code 3 working fine.
    Regards

    • Amit Sonkhiya

      Hello @unlockiphonecouk:disqus
      Thank you for notifying about this. This RewriteEngine on is required. Also I have shifted RewriteBase / upward in code section 3. Please check them & let me know if they are working fine

      • Ali Raza

        Thank you Amit for correction.
        Yes the new code 3 is also working good. but as you say that rewrite engine on line is necessary so maybe before site was loading from cloudflare cache but old and new both codes works fine for me.

        Regards

        • Amit Sonkhiya

          Thank you and welcome