开发者

mod_rewrite secure redirect

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]

But this doesn't:

RewriteCond %{SERVER_PORT} 80 
RewriteCond %{HTTP:Host} (mysite.localhost|mylivesite.com)
RewriteCond %{REQUEST_URI} (help/returns) 
Rewrite开发者_如何学CRule .? https://%1/%2/ [R=301,L]

The URL it tries to give me is https://help/returns//

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.

Most grateful for any input.


You can use this rule:

RewriteCond %{HTTPS} =off
RewriteRule ^help/returns https://%{HTTP_HOST}%{REQUEST_URI} [QSA,R=301,L]
  1. 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).

  2. 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).

  3. 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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜