mod_rewrite安全重定向 [英] mod_rewrite secure redirect
问题描述
与mod_rewrite进行斗争,试图将非安全页面重定向到安全页面.这有效:
Struggling with mod_rewrite trying to redirect a non-secure page to a secure one. This works:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} (help/returns)
RewriteRule .? https://mysite.localhost/%1/ [R=301,L]
但这不是:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP:Host} (mysite.localhost|mylivesite.com)
RewriteCond %{REQUEST_URI} (help/returns)
RewriteRule .? https://%1/%2/ [R=301,L]
它试图给我的URL是 https://help/returns//
The URL it tries to give me is https://help/returns//
我似乎无法使HTTP:host进入最后的RewriteRule行.
I can't seem to get the HTTP:host into the final RewriteRule line.
我需要主机,以便可以将同一文件用于本地开发和实时部署.
I need the host in there so I can use the same file for local dev and live deployment.
最感谢任何输入.
推荐答案
您可以使用以下规则:
RewriteCond %{HTTPS} =off
RewriteRule ^help/returns https://%{HTTP_HOST}%{REQUEST_URI} [QSA,R=301,L]
-
此规则会将所有发送到
http://example.com/help/returns
的请求重定向到安全(HTTPS)位置:https://example.com/help/returns
-它将保留完整的URL路径+查询字符串.您的条件太多,规则变得复杂,当服务器真正繁忙时(正则表达式非常昂贵),这不是一件好事.
This rule will redirect all requests to
http://example.com/help/returns
to a secure (HTTPS) location:https://example.com/help/returns
-- it will preserve full URL path + query string. You have too many conditions, rule becomes complex which is not a good thing when your server is REALLY busy (regular expressions are expensive).
我已经用更合适的%{HTTPS} =off
替换了%{SERVER_PORT} 80
(如果您的网站在非默认端口(80)上运行,这尤其有用).
I have replaced %{SERVER_PORT} 80
by more proper %{HTTPS} =off
(this especially useful if your site is run on non-default port, which is 80).
我还删除了HTTP_HOST匹配部分-除非您将多个域名/子域绑定到同一站点,否则您实际上不需要它.如果您需要此条件,只需在第一行之后添加此行:RewriteCond %{HTTP_HOST} ^(mysite.localhost|mylivesite.com)
I have also removed HTTP_HOST matching part -- you don't really need it unless you have more than one domain name/subdomain bound to the same site. In case if you need this condition just add this line after 1st line: RewriteCond %{HTTP_HOST} ^(mysite.localhost|mylivesite.com)
这篇关于mod_rewrite安全重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!