如何使用mod_rewrite有条件地重定向到https?

时间:2010-09-20 15:48:25

标签: mod-rewrite redirect https

我想仅在满足某些条件时才使用mod_rewrite重定向到https:

如果网址中未包含“管理员”字样 并且URL DOES包含字符串'xyz'(在URL的任何部分,包括查询字符串)

这似乎不起作用:

RewriteCond %{REQUEST_URI} xyz [NC,OR]
RewriteCond %{QUERY_STRING} xyz [NC]
RewriteCond %{REQUEST_URI} !administrator [NC]
ReWriteCond %{HTTPS} != on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R,L]

2 个答案:

答案 0 :(得分:1)

尝试此规则:

RewriteCond %{THE_REQUEST} !administrator
RewriteCond %{THE_REQUEST} xyz
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

THE_REQUEST 中测试request line更容易,因为它包含路径和查询。但请确保您的 xyz 不属于methodHTTP version

答案 1 :(得分:0)

我最终使用了编码解决方案,因为我无法使用mod_rewrite。 :(