htaccess 在特定查询参数上强制使用 https 并通常删除 www

htaccess force https on specific query param and remove www generally

感谢您对此提供的帮助。我试图在 url 中的查询参数标识的某个视图上强制使用 https。但是该规则似乎与强制删除 www 子域的其他规则冲突。

我的 .htaccess 目前看起来像这样:

RewriteCond %{QUERY_STRING} !(^|&)param=(foo)(&|$)
RewriteCond %{HTTPS} on
RewriteRule ^index\.php$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R]

RewriteCond %{QUERY_STRING} (^|&)param=(foo)(&|$)
RewriteCond %{HTTPS} off
RewriteRule ^index\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]

RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R=301,L]

然而,这会导致相关页面出现重定向循环。

尝试在前 2 条规则中使用 THE_REQUEST

RewriteCond %{THE_REQUEST} !/index\.php\?param=foo[&\s]
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R]

RewriteCond %{THE_REQUEST} /index\.php\?param=foo[&\s]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]

RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R=301,L]

确保清除浏览器缓存以测试此更改。