简体   繁体   中英

Redirect specific sub domains to https in .htaccess with mod_rewrite

I'm trying to test out SSL on my server starting with a TEST environment. The rules seem to work on my local, but when I commit to my test server I get the "Too many redirects" error.

Here are the relevant rewrite conditions/rules i have:

# Send non-www to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Send test.mysite.com to https
RewriteCond %{HTTPS} off
#RewriteCond %{HTTP_HOST} ^(.+)\.test\.mysite\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.test\.mysite\.com$ [NC]
RewriteRule ^ https://www.test.mysite.com%{REQUEST_URI} [L,R=301]

The goal is to send www.test.mysite.com and test.mysite.com both to https://www.test.mysite.com

I've uncommented the line:

RewriteCond %{HTTP_HOST} ^(.+)\.test\.mysite\.com$ [NC]

And it seems to be working now in private browsers. Does this make the www line unnecessary?

RewriteCond %{HTTP_HOST} ^www\.test\.mysite\.com$ [NC]

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