简体   繁体   中英

.htaccess redirect all http to https and one the other way

I need to redirect all requests but one to https, and that one if accessed with https should be redirected to http. Additionally I'm need standard php rewrite so all lands in index.php . Redirecting all to https is easy. I can make it work one way https -> http, but if I uncomment those lines I will get redirected from http://example.com/bye to https://example.com/index.php . What am I missing ?

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/bye$
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L]

#RewriteCond %{HTTPS} off
#RewriteCond %{REQUEST_URI} !^/bye$
#RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

You need to use THE_REQUEST variable instead since REQUEST_URI variable changes to /index.php by your very last rule.

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} \s/+bye/?[?\s]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !\s/+bye/?[?\s]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM