【问题标题】:Redirecting both http and https requests to new host将 http 和 https 请求都重定向到新主机
【发布时间】:2017-06-08 13:12:23
【问题描述】:

在 Apache 2.4.6 中,我想将来自 http://A.org/foohttps://A.org/foo 的请求重定向到 https://B.org/foo

我正在使用以下指令:

<VirtualHost 1.2.3.4:80>
     ServerName B.org
     ServerAlias A.org
     RewriteEngine on
     RewriteCond %{HTTP_HOST} !^B.org$
     RewriteRule ^/(.*)$ https://B.org/$1 [R=permanent,L]
</VirtualHost>

<VirtualHost 1.2.3.4:443>
     ServerName B.org
     ServerAlias A.org
     RewriteEngine on
     RewriteCond %{SERVER_PORT} ^443(s)
     RewriteCond %{HTTP_HOST} !^B.org$
     RewriteRule ^/(.*)$ https://B.org/$1 [R=permanent,L]
</VirtualHost>

当我访问http://A.org/foo 时,它会重定向到https://B.org/foo(正确)。

当我访问 https://A.org/foo 时,这会加载 https://B.org/foo 但不会重写 URL。所以我从网络浏览器收到 SSL 证书域不匹配错误。

第二个VirtualHost 指令是否有问题会阻止 URL 被重写?

【问题讨论】:

  • 注释掉RewriteCond %{SERVER_PORT} ^443(s)这一行,清除浏览器缓存后重新测试。
  • 我得到了同样的行为。

标签: apache .htaccess redirect ssl


【解决方案1】:

必须在发出重定向之前验证 SSL 证书,因此您需要在从 https://A.org/ 发出重定向的 VirtualHost 中使用对“A.org”有效的证书,并将其分开来自为 https://B.org/ 服务的 VirtualHost。

线路检查服务器端口不是必需的,因为 VirtualHost 无论如何只服务于该端口。只需将其删除。

这里有一个大纲,规则也稍微调整了美学,你可能喜欢也可能不喜欢!

<VirtualHost 1.2.3.4:80>
     ServerName B.org
     ServerAlias A.org
     RewriteEngine on
     RewriteCond %{HTTP_HOST} !=B.org
     RewriteRule ^(.*)$ https://B.org$1 [R=permanent,L]
</VirtualHost>

<VirtualHost 1.2.3.4:443>
     ServerName A.org
     RewriteEngine on
     RewriteRule ^(.*)$ https://B.org$1 [R=permanent,L]
</VirtualHost>

<VirtualHost 1.2.3.4:443>
     ServerName B.org
</VirtualHost>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 2021-07-27
    • 2014-06-30
    • 1970-01-01
    相关资源
    最近更新 更多