简体   繁体   中英

Do I need to modify this mod-rewrite code if I add an SSL certificate to my server?

This is a follow-up to a question I asked last year about setting up mod-rewrites: http://bit.ly/h8PVd9

I have the following mod-rewrite on my server which forces all traffic to use the full URL ( for the sake of this question, I'll use www.mysite.com ). It's working fine and there are no issues:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC]
RewriteCond %{HTTP_HOST} !^dev\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]

So that's all well and good, however I need to add an SSL to the server, covering the domain www.mysite.com . What (if any) modification/exception do I need to add to my pre-existing rewrite code to ensure any calls to https ://www.mysite.com won't be redirected to http://www.mysite.com ? Do I need to revise anything at all?

I'm not a rewrite expert, but I think below will work.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC]
RewriteCond %{HTTP_HOST} !^dev\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]

After testing and consulting with my webhost, the answer to this question is the following:

RewriteCond %{HTTPS_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ https://mysite.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,L]

The first rule addresses all https requests to the server and allows them through without getting redirected by my original rules. The second is a re-write of my original rules, and addresses the forced use of www.mysite.com regardless of what the user types in.

@Paul - you were pretty close on this one, but with my existing re-writes this solution solves both issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM