如果通过 htaccess 缺少查询字符串参数,请将 HTTPS 重定向到 HTTP


Redirect HTTPS to HTTP if querystring parameter is absent via htaccess

我想仅在缺少以下查询字符串参数时才将HTTPS重定向到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

也就是说,除这些查询字符串之外的任何查询字符串都应从 HTTPS 重定向到 HTTP。我想确保只有我的结帐路径是HTTP。

这可以简单地使用带有RewriteCondRewriteRule.htaccess来完成吗?

将其添加到文档根目录中的 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,然后将此代码放在DOCUMENT_ROOT目录下的.htaccess中:

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]