Apache 重写更改URL而不是映射到文件的规则

Apache 重写更改URL而不是映射到文件的规则,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我遇到了一种奇怪的行为,我无法解释也无法纠正。我需要将每个HTTP请求重定向到HTTPS。我使用了以下代码: RewriteEngine On RewriteBase / RewriteCond %{HTTPS} off RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L] RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCon

我遇到了一种奇怪的行为,我无法解释也无法纠正。我需要将每个
HTTP
请求重定向到
HTTPS
。我使用了以下代码:

RewriteEngine On
RewriteBase /

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

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# The query string in the rewrite is for testing purposes
RewriteRule (.*) /index.php?url=$1&%{REQUEST_URI}&http=%{HTTPS} [L]
到目前为止,它是有效的。然后,我需要将单个页面设置为
HTTP
,因此我添加了一些重写条件:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/not-https
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/not-https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?url=$1&%{REQUEST_URI}&https=%{HTTPS} [L]
现在,发生了什么。由于某些原因,当访问
/not https
页面时,它会重定向到
/index.php?url=not https&/not https&https=off

下面是GET请求的映射,后跟重定向/显示的URL

GET: http://example.com/test
  ->  https://example.com/test with proper $_GET

GET: http://example.com/test.jpg
  ->  https://example.com/test.jpg with no $_GET (file exists)

GET: https://example.com/not-https
  ->  http://example.com/not-https
  ->  http://example.com/index.php?url=not-https&/not-https&https=off

我的问题是为什么
而不是https
会更改显示的URL(从而使我的应用程序一团糟)?

之所以发生这种情况,是因为
请求URI
变量的值在生成条件
的最后一条规则中更改为
/index.php?..
^/非https
在第二条规则中成功并使其执行该规则

将前两条规则更改为:

RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} \s/+not-https [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !\s/+not-https [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

REQUEST\u URI
variable
不同,在执行其他内部重写后,请求的值不会改变。

非常感谢,这解决了所有问题。我猜,
REQUEST\u URI
在某种程度上发生了变化,但我在文档中找不到任何东西。另外,我发现调试
.htaccess
文件非常困难。。。