htaccess force https阻止下游代理规则

时间:2018-12-20 18:36:42

标签: .htaccess

我正在使用htaccess重写规则将子目录值重定向为变量:

Options -Indexes

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/members/*
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.+[^/])(.*)$ http://example.com/index.php?username=$1 [P,L]

这很好用,并使用了P标志,因此浏览器中显示的URL不会重定向。但是,当我添加代码以强制域通过https ...

运行时
Options -Indexes

RewriteEngine on

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/members/*
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.+[^/])(.*)$ http://example.com/index.php?username=$1 [P,L]

...这将导致浏览器中的URL更改。例如:

https://example.com/myvariable

在浏览器地址栏中重定向到:

https://example.com/index.php?username=myvariable

它确实可以成功强制执行SSL,但是如何在不更改URL的情况下同时完成这两项操作?

1 个答案:

答案 0 :(得分:0)

经过大量测试后,我确定可以从重写规则中删除域以及[P]标志,并且可以正常工作:

Options -Indexes

RewriteEngine on

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/members/*
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.+[^/])(.*)$ /index.php?username=$1 [L]