如何仅让主域名实现HTTP转HTTPS重定向,排除附加域名?
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
RewriteCondchecks if the requested domain exactly matches your main domain. The[NC]flag makes this check case-insensitive, so it works for variations likeYour-Main-Domain.comtoo. - We keep the existing
RewriteCondto verify the connection isn't over HTTPS. - The
RewriteRulenow 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

