我一直在寻找一段可以强制使用 https://
的代码片段。我发现了这个:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
问题是当我想在本地工作时,像“localhost”或“example.dev”这样的 url 它会尝试重定向到 https://localhost
(它不起作用) .
我该如何解决?
(总而言之,我希望上面的代码片段仅在我在生产环境中使用时才有效,例如“example.com”)
最佳答案
I want the snippet above to work only when I use it on production, e.g
example.com
)
您可以使用重写条件将您的规则限制为仅针对 example.com
:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
关于regex - 在 .htaccess 上强制使用 HTTPS,但仅限于生产环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25818301/