Apache环境下主域名及子域名转非www重定向问题求助
问题背景
public_html 目录结构
main index.php subdomain1 index.php .htaccess subdomain2 index.php .htaccess
根目录 .htaccess 现有配置
RewriteEngine on # 将 www 重定向到非 www RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ https://%1/$1 [R=301,L] # 将主域名指向 main 目录 RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !^/main/ [NC] RewriteRule ^(.*)$ /main/$1 [L]
当前状态:
example.com已正确指向public_html/main目录内容example.com和www.example.com可正常重定向到非 www 的主域名- 问题:
www.subdomain1.example.com和www.subdomain2.example.com无法重定向到对应的非 www 子域名
尝试过的操作:在 subdomain2 目录创建 .htaccess,配置如下,但未生效,还出现「连接不是私密连接」及「NET::ERR_CERT_COMMON_NAME_INVALID」错误:
RewriteEngine On RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
环境说明:Apache 2.4.58,已为每个子域名配置 CNAME 记录,例如:
CNAME www.subdomain2 subdomain2.example.com.
解决方案
一、修复 .htaccess 方案(解决重定向+证书问题)
1. 先解决证书错误
出现证书错误的核心原因是你的 SSL 证书未包含 www.subdomain1.example.com 和 www.subdomain2.example.com 这两个域名。你需要:
- 申请包含泛域名(如
*.example.com)的 SSL 证书,或者为每个子域名的 www 变种单独添加证书 Subject Alternative Name(SAN) - 确保 Apache 配置中,子域名的虚拟主机(VirtualHost)正确绑定了覆盖 www 子域名的证书
2. 调整根目录 .htaccess 配置
当前根目录的重定向规则被主域名的转发规则覆盖了。修改根目录 .htaccess,把 www 重定向规则放在主域名转发规则之前,确保规则能匹配所有子域名的 www 前缀:
RewriteEngine on # 全局处理所有域名的 www -> 非 www 重定向(优先级最高) RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ https://%1/$1 [R=301,L] # 主域名指向 main 目录的规则 RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !^/main/ [NC] RewriteRule ^(.*)$ /main/$1 [L]
注意:如果子域名有独立的 VirtualHost 配置,需确保配置了
AllowOverride All以允许.htaccess生效
二、不使用 .htaccess,通过 Apache 主配置实现(推荐)
直接在 Apache 虚拟主机配置文件中设置规则,比 .htaccess 效率更高、更易维护:
1. 主域名虚拟主机配置
<VirtualHost *:80> ServerName example.com ServerAlias www.example.com # HTTP 强制跳转到 HTTPS Redirect permanent / https://example.com/ </VirtualHost> <VirtualHost *:443> ServerName example.com ServerAlias www.example.com DocumentRoot /path/to/public_html/main # SSL 证书配置 SSLEngine on SSLCertificateFile /path/to/cert.crt SSLCertificateKeyFile /path/to/private.key SSLCertificateChainFile /path/to/chain.crt # 如有链式证书需添加 # www -> 非 www 重定向 RewriteEngine On RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC] RewriteRule ^(.*)$ https://example.com/$1 [R=301,L] </VirtualHost>
2. 子域名虚拟主机配置(以 subdomain2 为例)
<VirtualHost *:80> ServerName subdomain2.example.com ServerAlias www.subdomain2.example.com Redirect permanent / https://subdomain2.example.com/ </VirtualHost> <VirtualHost *:443> ServerName subdomain2.example.com ServerAlias www.subdomain2.example.com DocumentRoot /path/to/public_html/subdomain2 # SSL 证书配置(必须覆盖 www.subdomain2.example.com) SSLEngine on SSLCertificateFile /path/to/cert.crt # 使用泛域名证书或包含该子域名的 SAN 证书 SSLCertificateKeyFile /path/to/private.key SSLCertificateChainFile /path/to/chain.crt # www -> 非 www 重定向 RewriteEngine On RewriteCond %{HTTP_HOST} ^www\.subdomain2\.example\.com$ [NC] RewriteRule ^(.*)$ https://subdomain2.example.com/$1 [R=301,L] </VirtualHost>
3. 关键注意事项
- 每个子域名都需配置对应的
VirtualHost,并添加ServerAlias匹配 www 前缀的域名 - 确保 SSL 证书覆盖所有需要使用的域名(主域名、子域名、www 变种)
- 修改配置后执行
apachectl configtest检查语法,再重启 Apache 服务(apachectl restart)
内容的提问来源于stack exchange,提问作者Dev
相关产品推荐
相关产品推荐

