Redirect HTTPS to HTTP if querystring parameter is absent via htaccess

对着背影说爱祢 提交于 2020-01-14 04:48:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!