Reputation: 169
UPDATE: After hours of searching still had no solution so I had to change/update my question:
I have few pages with parameters manualy converted to .htm using this code:
RewriteRule ^MyPage1.htm$ cart.php?rid=1 [L,NC]
I have tested diffrent solutions and non of them works fully:
CODE 1:
RewriteCond %{HTTPS} off
RewriteCond %{ENV:HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]
RESULT 1:
http://example.com/randompage.php
-> http://example.com/randompage.php
[NOT WORKING]
http://www.example.com/randompage.php
-> https://example.com/randompage.php
[WORKING]
https://www.example.com/randompage.php
-> https://example.com/randompage.php
[WORKING]
http://example.com/MyPage1.htm
-> http://example.com/MyPage1.htm
[NOT WORKING]
http://www.example.com/MyPage1.htm
-> https://example.com/MyPage1.htm
[WORKING]
https://www.example.com/MyPage1.htm
-> https://example.com/MyPage1.htm
[WORKING]
CODE 2
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{http_host} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
CODE 3
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteCond %{http_host} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
RESULT 2 & 3 (Same results)
http://example.com/randompage.php
-> https://example.com/randompage.php
[WORKING]
http://www.example.com/randompage.php
-> https://example.com/randompage.php
[WORKING]
https://www.example.com/randompage.php
-> https://example.com/randompage.php
[WORKING]
http://example.com/MyPage1.htm
-> http://example.com/MyPage1.htm
[NOT WORKING]
http://www.example.com/MyPage1.htm
-> https://example.com/MyPage1.htm
[WORKING]
https://www.example.com/MyPage1.htm
-> https://example.com/MyPage1.htm
[WORKING]
Upvotes: 0
Views: 176
Reputation: 1768
Just use the OR operator with a single protocol and domain specific rule:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) https://example.com/$1 [L,R=301,NE]
Upvotes: 0
Reputation: 700
As Mike Rockett mentioned in the comment, make sure the redirect rule is before the RewriteRule ^MyPage1.htm$ cart.php?rid=1 [L,NC]
. You don't want the [L]
stopping evaluation of later rules.
If that doesn't do the trick, try turning on advanced logging by putting LogLevel trace6
in your .htaccess and then look at the Apache error log to see what is actually happening with the rewrite rules.
Also, another thing that can mess you up in development of redirects is that they can get cached, so even if you fix the problem, things are still broken. I'm not saying that's likely the case in this situation but just something to be aware of. I recommend just using [R]
instead of [R=301]
until you've confirmed it's working to avoid this issue. If you suspect caching, you can try opening another browser that you haven't used yet on this like IE / Firefox / Chrome.
Upvotes: 2