Reputation: 24758
I want to redirect http to https and www to non www.
Cases:
http://example.com/my/url
http://www.example.com/my/url
https://example.com/my/url
https://www.example.com/my/url
Result
https://example.com/my/url
https://example.com/my/url
https://example.com/my/url
https://example.com/my/url
My current htaccess file:
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) https://%1/$1 [R=301,NE,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
Problem:
It redirects to https://example.com/index.php.
I want it to respect the url https://example.com/my/url.
I've found many questions but I have not yet found a complete solution. In fact, the htaccess code above was the only one that worked quite well out of like 10 tries.
Upvotes: 1
Views: 280
Reputation: 785108
You can use this single rule for removing www
and http -> https
:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
# rest of rules go here ....
If you have an issue with too many redirects, try to replace the second line with:
RewriteCond %{ENV:HTTPS} !on
It is important to keep this rule as your very first rule and clear your browser cache before testing.
Upvotes: 2