如果通过htaccess查询string参数不存在,则将HTTPSredirect到HTTP

只想在以下查询string参数不存在的情况下,将HTTPSredirect到HTTP:

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

也就是说,除这些之外的任何查询string都应该从HTTPSredirect到HTTP。 我想确保只有我的结帐path是HTTP。

这可以简单地使用.htaccessRewriteCondRewriteRule

将其添加到文档根目录中的htaccess文件中,最好在可能已存在的任何规则之上:

 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] 

要使其成为永久重定向,请在方括号中的R之后添加=301

通过httpd.conf启用mod_rewrite和.htaccess,然后把这个代码放在你的.htaccess下的DOCUMENT_ROOT目录下:

 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] 

把下面的行放到你的.htaccess文件中:

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