问题
I would like to redirect HTTPS to HTTP only if the following querystring parameters are absent:
https://www.example.com/index.php?p=login
https://www.example.com/index.php?p=signup
https://www.example.com/index.php?p=cart
https://www.example.com/index.php?p=one_page_checkout
That is, any querystring other than these should redirect from HTTPS to HTTP. I want to make sure only my checkout path is HTTP.
Can this be done simply using .htaccess
with RewriteCond
and RewriteRule
?
回答1:
Add this to the htaccess file in your document root, preferably above any rules that may already be there:
RewriteEngine On
RewriteCond %{QUERY_STRING} !(^|&)p=(login|signup|cart|one_page_checkout)(&|$)
RewriteCond %{HTTPS} on
RewriteRule ^index\.php$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R]
RewriteCond %{QUERY_STRING} (^|&)p=(login|signup|cart|one_page_checkout)(&|$)
RewriteCond %{HTTPS} off
RewriteRule ^index\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
To make this a permanent redirect, add a =301
after the R
in the square brackets.
回答2:
Enable mod_rewrite and .htaccess through httpd.conf
and then put this code in your .htaccess
under DOCUMENT_ROOT
directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} on
RewriteCond %{QUERY_STRING} !(^|&)p=(login|signup|cart|one_page_checkout)(&|$) [NC]
RewriteRule ^(index\.php)?$ http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
回答3:
Just put below lines into your .htaccess file :-
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
来源:https://stackoverflow.com/questions/18854658/redirect-https-to-http-if-querystring-parameter-is-absent-via-htaccess