只有当查询参数不等于某个值时,HTTPS才会在htaccess中redirect

背景

我设法通过在我的.htaccess文件中包含这个代码片段来让httpredirect工作在我的Wordpress安装中:

 <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] </IfModule> 

我在Google Cloud环境中运行,问题是运行状况检查要求“健康检查资源”返回200 OK 。 运行状况检查会发出一个不包含X-Forwarded-Proto头部信息的HTTP请求,所以Apache将以301做出响应,因此运行状况检查失败(不遵循redirect)。 不幸的是,在Google Health Checker中定义头文件是不可能的,所以我不能将X-Forwarded-Proto头文件伪装成“https”。 所以我希望有一个解决方法是在运行状况检查path中提供查询参数,并将.htaccess文件configuration为只有在指定的查询参数未定义(或等于false或类似)时才redirect。

如果X-Forwarded-Proto头部不等于https (如上例所示)并且查询string不包含参数healthCheck=true那么如何修改RewriteCond (我假设)只执行redirect?

你可以试试这个吗?

 <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteCond %{QUERY_STRING} !.*healthCheck=true.* RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] </IfModule> 

看看在https://stackoverflow.com/questions/4824964/rewritecond-in-htaccess-with-negated-regex-condition-doesnt-work和https://wiki.apache.org/httpd/RewriteQueryString