通过 .htaccess 开启 HTTPS

通过 .htaccess 开启 HTTPS

我目前有一个网站,我正在使用 .htaccess 为网站的某些部分启用 https:

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (evaluate/purchase*)
RewriteRule (.*) https://mydomain.com%{REQUEST_URI} 

“evaluate”文件夹中有多个以“purchase”开头的文件,所有这些文件都需要保护。到目前为止,此方法有效。

我现在需要保护其他几个文件和目录,但将它们添加为重写条件似乎不起作用:

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (evaluate/purchase*)
RewriteCond %{REQUEST_URI} (another_dir/file.php)
RewriteCond %{REQUEST_URI} (please_secure_me.php)
RewriteRule (.*) https://mydomain.com%{REQUEST_URI} 

这不会产生任何 500 或任何其他东西,它只会在第一种情况下保护文件。我做错了什么?

答案1

IIRC,RewriteCond 是一个 AND 条件。

“如果所有条件都匹配,则继续处理,并使用替换字符串替换 URL。”

您现在所说的是(HTTPS 关闭 AND URI 是这个 AND URI 是这个 AND URI 是这个)这是不正确的,因为 URI 不能同时是 3 个不同的东西!

您需要一个组合的 AND/OR 条件(HTTPS 关闭 AND(URI 是这个 OR URI 是这个 OR URI 是这个))

尝试复制您的规则:

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (evaluate/purchase*)
RewriteRule (.*) https://mydomain.com%{REQUEST_URI} 

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (another_dir/file.php)
RewriteRule (.*) https://mydomain.com%{REQUEST_URI} 

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (please_secure_me.php)
RewriteRule (.*) https://mydomain.com%{REQUEST_URI} 

答案2

实际上,只需浏览 2.2 文档,您就可以这样做:

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (evaluate/purchase*) [OR]
RewriteCond %{REQUEST_URI} (another_dir/file.php) [OR]
RewriteCond %{REQUEST_URI} (please_secure_me.php)
RewriteRule (.*) https://mydomain.com%{REQUEST_URI} 

如果可行,这将是一个更为优雅的解决方案。

相关内容