除了登录和注册页面,我想将 https 请求重定向到 http。
所以 https://example.com 会重定向到 http://example.com 但 example.com/user/login 应该总是重定向到 https://example.com/user/login .
如何在我的 .htaccess 文件中执行此操作?
最佳答案
您可以使用 mod_rewrite 中的 %{HTTPS}
变量检测当前请求是否使用 https。这是 on
或 off
。请注意,您想要的重定向不会有帖子请求的预期行为,因为重定向的 url 将始终使用没有帖子正文的 get 来请求。
RewriteEngine on
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/user/(login|register)$
RewriteRule ^(.*)$ http://example.com/$1 [R,L]
RewriteCond %{HTTPS} =off
RewriteRule ^user/(login|register)$ https://example.com/user/$1 [R,L]
在测试重定向按预期工作后,将 R
标志更改为 R=301
。
关于.htaccess - 将某些页面的 https 重定向到 http,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26562679/