添加SSL证书后服务器出现异常重定向问题
解决HTTP访问lauraswork.co.uk时异常重定向至测试子域名的问题
问题描述
服务器部署了域名lauraswork.co.uk,完成迁移并添加SSL证书后,https://lauraswork.co.uk访问正常,但访问http://lauraswork.co.uk时会被异常重定向至lauraswork.mytestsubdomain.co.uk,触发SSL错误。站点为WordPress,数据库中未找到该测试子域名的相关记录。
当前Apache虚拟主机配置
80端口(HTTP)配置
<VirtualHost *:80> # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. ServerName lauraswork.co.uk ServerAlias www.lauraswork.co.uk ServerAdmin simon@email.co.uk DocumentRoot /var/www/lauraswork.co.uk/public_html # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular # modules, e.g. #LogLevel info ssl:warn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. For example the # following line enables the CGI configuration for this host only # after it has been globally disabled with "a2disconf". #Include conf-available/serve-cgi-bin.conf RewriteEngine on RewriteCond %{SERVER_NAME} =lauraswork.co.uk [OR] RewriteCond %{SERVER_NAME} =www.lauraswork.co.uk RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] </VirtualHost>
443端口(HTTPS)配置
<IfModule mod_ssl.c> <VirtualHost *:443> # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. ServerName lauraswork.co.uk ServerAlias www.lauraswork.co.uk ServerAdmin simon@myemail.co.uk DocumentRoot /var/www/lauraswork.co.uk/public_html # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular # modules, e.g. #LogLevel info ssl:warn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. For example the # following line enables the CGI configuration for this host only # after it has been globally disabled with "a2disconf". #Include conf-available/serve-cgi-bin.conf Include /etc/letsencrypt/options-ssl-apache.conf SSLCertificateFile /etc/letsencrypt/live/lauraswork.co.uk/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/lauraswork.co.uk/privkey.pem </VirtualHost> </IfModule>
排查与解决方案
1. 检查Apache全局配置及其他虚拟主机
- 查看默认虚拟主机配置(如
/etc/apache2/sites-available/000-default.conf),确认是否存在指向测试子域名的重定向规则 - 执行命令列出所有启用的虚拟主机,检查是否有其他配置冲突:
确认apache2ctl -S # Debian/Ubuntu # 或 httpd -S # CentOS/RHELlauraswork.co.uk的80端口是否被当前配置独占,以及是否存在lauraswork.mytestsubdomain.co.uk的虚拟主机配置导致默认匹配。
2. 检查WordPress根目录的.htaccess文件
进入站点根目录/var/www/lauraswork.co.uk/public_html,查看.htaccess内容,删除任何指向测试子域名的重定向规则。如果需要保留HTTPS强制跳转,可使用以下标准规则:
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
3. 清除缓存并测试重定向
- 使用浏览器无痕模式访问
http://lauraswork.co.uk,避免旧301缓存干扰 - 用curl命令追踪重定向流程,确认重定向来源:
观察输出中的curl -v http://lauraswork.co.ukLocation头,确定是哪一步触发了测试子域名的跳转。
4. 检查Let's Encrypt的SSL配置文件
打开/etc/letsencrypt/options-ssl-apache.conf,确认文件中无异常重定向规则。该文件应为SSL优化配置,不应包含域名跳转逻辑。
5. 验证配置并重启Apache
修改配置后,先验证语法正确性:
apache2ctl configtest # Debian/Ubuntu # 或 httpd configtest # CentOS/RHEL
确认无错误后重启服务:
systemctl restart apache2 # Debian/Ubuntu # 或 systemctl restart httpd # CentOS/RHEL
内容的提问来源于stack exchange,提问作者Udders
相关产品推荐
相关产品推荐

