Reputation: 3185
How can I redirect only main page to https and without www?
http://www.mywebsite.com
http://mywebsite.com
https://www.mywebsite.com
All of URLs above should redirect once to:
https://mywebsite.com
Everything inside subdirectories should stay intact
https://mywebsite.com/website-in-progress
I am trying various code I have found but I didn't find anything to work all cases.
EDIT: This seems to be working visually great, the problem is that some URLs have multiple redirects
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
The outcome of this is:
- https://mywebsite.com/ - OK
- https://www.mywebsite.com/ - not desirable, redirects first to http://mywebsite.com and then to https://mywebsite.com
- http://www.mywebsite.com/ - not desirable, redirects first to https://www.mywebsite.com and then to https://mywebsite.com
- http://mywebsite.com/ - OK, redirects once to https://mywebsite.com/
Upvotes: 0
Views: 45
Reputation: 785276
You may use this rule as top most rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^/?$ https://%1% [R=301,L]
Upvotes: 1