You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

主站已强制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 skip www.xxx.domain.com from 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 the www. check and HTTPS off check in the third rule. This ensures the main site’s HTTPS enforcement only applies to your primary domain, not xxx.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 .htaccess overrides (check for AllowOverride All in your site’s virtual host settings).
  • Clear your browser cache before testing to avoid old redirects interfering with results.

内容的提问来源于stack exchange,提问作者johnohod

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:14:09