将HTTP和https请求都重定向到新主机 [英] Redirecting both http and https requests to new host
问题描述
在Apache 2.4.6中,我想从 http://A.org/foo
和 https:// A重定向请求.org / foo
到 https://B.org/foo
。
In Apache 2.4.6, I would like to redirect requests from http://A.org/foo
and https://A.org/foo
to 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
(正确)。
When I visit http://A.org/foo
, this redirects to https://B.org/foo
(correct).
何时我访问 https://A.org/foo
,此加载 https://B.org/foo
但不重写URL。因此,我从网络浏览器中收到SSL证书域不匹配错误。
When I visit https://A.org/foo
, this loads https://B.org/foo
but does not rewrite the URL. So I get an SSL certificate domain mismatch error from the web browser.
第二个 VirtualHost
出现问题吗?
推荐答案
必须先验证SSL证书,然后才能发布重定向,因此,可以防止重新编写URL。您将需要使用从 https://A.org/
发出重定向的VirtualHost中对'A.org'有效的证书,并将其分开从服务 https://B.org/
的VirtualHost中获得。
The SSL certificate has to be validated before the redirect can be issued, so you will need to use a certificate that is valid for 'A.org' within the VirtualHost that is issuing the redirect from https://A.org/
, and separate it from the VirtualHost serving https://B.org/
.
不需要行检查服务器端口,因为VirtualHost仍然只为该端口提供服务。只需删除它即可。
The line checking server port is not necessary because the VirtualHost only serves that port anyway. Just remove it.
这里是一个轮廓,其中的规则也针对美学进行了一些微调,您可能会或可能不喜欢它!
Here's an outline with the rules also slightly tweaked for aesthetics which you may or may not prefer!
<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>
这篇关于将HTTP和https请求都重定向到新主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!