【问题标题】:.htaccess redirecting non SSL Domain to another SSL Domain.htaccess 将非 SSL 域重定向到另一个 SSL 域
【发布时间】:2018-10-18 10:07:40
【问题描述】:

我有以下 .htaccess:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.old_domain.de$
RewriteRule (.*) https://new_domain.de/$1 [R=301,L]

问题是,该规则似乎只正确重定向 www.old_domain.de 或 http://www.old_domain.de,但没有重定向 https://old_domain.de 或 old_domain.de(自动跳转到 https://)。在第二种情况下,我收到以下错误:

SSL_ERROR_UNRECOGNIZED_NAME_ALERT

我已经尝试将 https:// 和非 www 域重写为 http://www。但无法以某种方式达到想要的结果。可能我在这里错过了一些重要的事情。

在我的新域的 .htaccess 中强制 https:// 可能有问题吗?

【问题讨论】:

  • 嗨 QCube。我已经为您的问题提供了有效的解决方案以及测试人员。如果有任何问题,请发表评论,或者如果这解决了您的问题,请投票并接受答案,以便其他 SO 用户可以从中受益。
  • 好的,非常感谢!

标签: apache .htaccess https ssl-certificate url-redirection


【解决方案1】:

对于重定向 http://www.old_domain.de 或 www.old_domain.de 或 http://old_domain.de 或 old_domain.de 使用以下代码

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?old_domain\.de$ [NC]
RewriteRule (.*) https://new_domain.de/$1 [R=301,L]

您的代码在 RewriteCond 中缺少 (www\.)?,这将使重写工作正常,无论它是否包含 www。

您可以在link 使用 htaccess 测试器进行测试

同样,您应该为 SSL (HTTPS) 配置单独的虚拟主机。在该虚拟主机中,您还需要复制上述代码。

在端口 443 上配置 SSL 进行侦听,因此在您的 htaccess 中搜索类似于以下代码的内容

<VirtualHost *:443>

    ServerName www.old_domain.de
    ServerAlias old_domain.de

    SSLEngine on
    SSLCertificateFile /etc/apache2/ssl/example/apache.crt
    SSLCertificateKeyFile /etc/apache2/ssl/example/apache.key 

 </VirtualHost>

如果您仍然无法找到配置了虚拟主机监听 443 的文件,您可以使用 -S 标志 像下面的命令运行 apache

httpd -S or apachectl -S

这将为您提供配置监听 443 的虚拟主机的文件的名称,如下面的输出所示

*:80     default server xxx.xxx.xxx.xxx (/etc/httpd/conf.d/eco.conf:1)
         port 80 namevhost xxx.xxx.xxx.xxx (/etc/httpd/conf.d/eco.conf:1)

*:443    default server xxx.xxx.xxx.xxx (/etc/httpd/conf.d/ssl.conf:1)
         port 443 namevhost xxx.xxx.xxx.xxx (/etc/httpd/conf.d/ssl.conf:1)

例如/etc/httpd/conf.d/ssl.conf是配置虚拟主机监听443的文件。

【讨论】:

  • 感谢您的回答。它解决了到目前为止我在代码中遇到的一个问题。我不知道我对 SSL 虚拟主机的理解是否正确。我的 old_domain 没有 SSL 证书,并且由于我的 webspace 正在托管,我无法访问服务器或命令行或类似的东西(很遗憾)。但是新域确实有 SSL,但我的 Hoster 正在配置我猜你描述的部分。
  • 我认为您的问题的第一部分已经解决。所以现在您可以重定向所有不包含 https 的 url。对于问题的第二部分,您是否能够通过 https://old_domain.de 或 https://www.old_domain.de 等 https 访问 old_domain.de。如果不是,您不能将 https://old_domain.de 重定向到 https://new_domain.de。如果是,我们需要找到为 443 配置的虚拟主机。请告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-12
  • 1970-01-01
  • 1970-01-01
  • 2020-05-04
  • 2016-05-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多