简体   繁体   中英

Redirect HTTPS to HTTP if querystring parameter is absent via htaccess

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 ?

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.

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]

Just put below lines into your .htaccess file :-

RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

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