HTTPS 有时会重定向回 HTTP

HTTPS 有时会重定向回 HTTP

发生了奇怪的问题,需要帮助。

我有以下网址:

https://example.com/sslTest?bbb=&eeee=1

在某些随机时刻,如果我在浏览器 URL 栏上按下 Enter 键,它会重定向到 HTTP:

http://example.com/sslTest?bbb=&eeee=1

我正在使用铬:

这是我的.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

ErrorDocument 404 https://example.com/404.php

Options -Indexes

一切一直正常,似乎没有发生任何影响这一点的改变。另一个有趣的事实是,如果我删除查询,它会重定向回页面和 HTTPS。

我尝试添加:

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

可能是什么问题?页面返回错误400

编辑:如果我向空的 php 文件添加 url 查询,也会发生同样的问题

答案1

这看起来好像条件与您尝试重新路由到 HTTPS 的请求不匹配:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

ErrorDocument 404 https://example.com/404.php

Options -Indexes

来自文档: https://httpd.apache.org/docs/current/mod/mod_rewrite.html

The RewriteCond directive defines a rule condition. One or more RewriteCond can precede a RewriteRule directive. The following rule is then only used if both the current state of the URI matches its pattern, and if these conditions are met.

RewriteCond与请求不匹配的方式AND

http://example.com/sslTest?bbb=&eeee=1

RewriteCond %{REQUEST_FILENAME}.php -f

因此 RewriteRule 很可能不适用于该请求。


即使可能匹配,因为 REQUEST_FILENAME 最终可能会首先解析为包含.php结尾的某个文件。如果没有完整配置,则无法验证这一点。我认为您的设置中不是这种情况。

来自文档REQUEST_FILENAME

The full local filesystem path to the file or script matching the request,
if this has already been determined by the server at the time REQUEST_FILENAME
is referenced. Otherwise, such as when used in virtual host context, the same value as
REQUEST_URI. Depending on the value of AcceptPathInfo, the server may have only used 
some leading components of the REQUEST_URI to map the request to a file.

答案2

如果您希望为所有 URI 启用 HTTPS,请保持简单,如下所示:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

条件是:如果 https 没有启用。

重写规则匹配 uri 中的任何内容并将其发送给其 https 对应部分。

相关内容