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

如何仅让主域名实现HTTP转HTTPS重定向,排除附加域名?

How to Redirect Only Your Main Domain from HTTP to HTTPS in .htaccess

Got it, let's sort out this issue where your current .htaccess setup is redirecting all add-on domains to HTTPS along with your main domain. The problem with your existing code is that %{SERVER_NAME} matches any domain pointing to your server, so we need to add a condition to target only your main domain specifically.

Modified .htaccess Code (Target Only Main Domain)

# Enable rewrite engine
RewriteEngine On

# Target only your main domain (replace with your actual domain)
RewriteCond %{HTTP_HOST} ^your-main-domain\.com$ [NC]
# Check if connection is not using HTTPS
RewriteCond %{HTTPS} !=on
# Redirect to HTTPS version of the main domain
RewriteRule ^/?(.*) https://your-main-domain.com/$1 [R=301,L]

Breakdown of the Changes:

  • The first RewriteCond checks if the requested domain exactly matches your main domain. The [NC] flag makes this check case-insensitive, so it works for variations like Your-Main-Domain.com too.
  • We keep the existing RewriteCond to verify the connection isn't over HTTPS.
  • The RewriteRule now explicitly uses your main domain instead of %{SERVER_NAME}, which ensures only traffic to your main domain gets redirected.

Optional: Handle Both www and Non-www Main Domain Variations

If you want to redirect both your-main-domain.com and www.your-main-domain.com to a canonical HTTPS version, use this code instead:

RewriteEngine On

# Match either www or non-www version of your main domain
RewriteCond %{HTTP_HOST} ^(www\.)?your-main-domain\.com$ [NC]
RewriteCond %{HTTPS} !=on
# Redirect to your preferred canonical HTTPS version (adjust to non-www if needed)
RewriteRule ^/?(.*) https://www.your-main-domain.com/$1 [R=301,L]

Just remember to replace your-main-domain.com with your actual main domain in all instances. For testing, you can use [R,L] first (temporary redirect) and switch to [R=301,L] once confirmed (301 is permanent and better for SEO).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:25:16