主站已强制HTTPS,如何用.htaccess让子域名xxx.domain.com仅用HTTP?
Force Subdomain to HTTP While Main Site Uses HTTPS
To keep xxx.domain.com on HTTP while enforcing HTTPS for your main domain, you’ll need to adjust your .htaccess rules in two critical ways: redirect HTTPS traffic for the subdomain back to HTTP, and exclude the subdomain from your main site’s HTTPS enforcement logic.
Here’s the modified, commented .htaccess code tailored to your needs:
# Redirect HTTPS requests for xxx.domain.com to HTTP RewriteCond %{HTTPS} on RewriteCond %{HTTP_HOST} ^xxx\.domain\.com$ [NC] RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] # Redirect www main domain to non-www HTTPS (excludes www.xxx.domain.com) RewriteCond %{HTTP_HOST} ^www\.(?!xxx\.)(.*)$ [NC] RewriteRule ^(.*)$ https://%1/$1 [R=301,L] # Enforce HTTPS for main domain (skip xxx.domain.com) RewriteCond %{HTTP_HOST} !^xxx\.domain\.com$ [NC] RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR] RewriteCond %{HTTP_HOST} !^xxx\.domain\.com$ [NC] RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^(www\.)?(.+) RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]
Breakdown of Changes:
- HTTPS to HTTP Redirect for Subdomain: The first rule checks if the connection is HTTPS and the host is
xxx.domain.com(case-insensitive), then sends visitors to the HTTP version. The[L]flag ensures no other rules run after this redirect. - Exclude Subdomain from www-to-non-www: The second rule uses a negative lookahead
(?!xxx\.)to skipwww.xxx.domain.comfrom the main domain’s www-to-non-www redirect, preventing accidental HTTPS redirection for the subdomain’s www variant. - Skip Subdomain in HTTPS Enforcement: We added
RewriteCond %{HTTP_HOST} !^xxx\.domain\.com$ [NC]before both thewww.check andHTTPS offcheck in the third rule. This ensures the main site’s HTTPS enforcement only applies to your primary domain, notxxx.domain.com.
Pro Tips:
- Test with
[R=302](temporary redirect) first to avoid browser caching issues, then switch to[R=301]once you confirm everything works. - Verify your Apache config allows
.htaccessoverrides (check forAllowOverride Allin your site’s virtual host settings). - Clear your browser cache before testing to avoid old redirects interfering with results.
内容的提问来源于stack exchange,提问作者johnohod
相关产品推荐
相关产品推荐

