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).
1 2 3 4 5 6 7 | RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ http://%1/$1 [R=301,L] |
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.
1 2 3 4 5 6 7 8 9 10 11 12 | RewriteEngine on RewriteBase / RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ http://%1/$1 [R=301,L] RewriteCond %{HTTPS} on RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ https://%1/$1 [R=301,L] |
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:
1 2 3 4 5 6 7 8 9 10 11 | RewriteEngine on RewriteBase / RewriteCond %{HTTP_HOST} ^www\. RewriteCond %{HTTPS}s ^on(s)|off RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$ RewriteRule ^ %1%3%{REQUEST_URI} [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 regardless of http or https.
home page redirecting to www but another page not redirecting to www canonical error showing evermolpro.com please help
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
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
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
Thank you and welcome