简体   繁体   中英

htaccess redirects with subdomains and https

I'm working with a HostGator account that is hosting two domains (both using SSL). I also have subdomains set up on both of these that don't use the SSL. I have a redirect set up so that http will redirect to https, but I'm having problems working with the subdomains.

Here is what I need it to do:

http://site1.com -> https://site1.com

http://www.site1.com -> https://site1.com

dev.site1.com -> no redirect

http://site2.com -> https://site2.com

http://www.site2.com -> https://site2.com

dev.site2.com -> no redirect

What is happening now is that when I go to dev.site2.com (whether or not I put in https) what I see is https://site1.com , although it doesn't appear to be redirecting, because the address bar still shows dev.site2.com

Here is what I have in htaccess:

RewriteEngine On
RewriteBase /~hostgatorusername/
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteCond %{HTTP_HOST} !=dev.*
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]


# BEGIN WordPress
<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /~hostgatorusername/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /~hostgatorusername/index.php [L]
</IfModule>

# END WordPress

I'm still trying to fully grasp htaccess, so I'd love if someone could help me understand what I'm doing wrong. Thanks!

Update: I also need https://www.site1.com to redirect to https://site1.com and https://www.site2.com to redirect to https://site2.com

Try this code:

# BEGIN WordPress
<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /~hostgatorusername/

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^dev\. [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* https://%1%{REQUEST_URI} [R=301,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /~hostgatorusername/index.php [L]
</IfModule>

# END WordPress

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