Apache 将http和https请求重定向到新主机
在Apache2.4.6中,我想重定向来自Apache 将http和https请求重定向到新主机,apache,.htaccess,redirect,ssl,Apache,.htaccess,Redirect,Ssl,在Apache2.4.6中,我想重定向来自http://A.org/foo和https://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
http://A.org/foo
和https://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>
服务器名B.org
服务器别名A.org
重新启动发动机
重写cond%{HTTP_HOST}^B.org$
重写规则^/(.*)$https://B.org/$1[R=永久性,L]
服务器名B.org
服务器别名A.org
重新启动发动机
重写cond%{SERVER_PORT}^443(s)
重写cond%{HTTP_HOST}^B.org$
重写规则^/(.*)$https://B.org/$1[R=永久性,L]
当我访问http://A.org/foo
,这将重定向到https://B.org/foo
(正确)
当我访问https://A.org/foo
,这将加载https://B.org/foo
但不重写URL。因此,我从web浏览器中得到一个SSL证书域不匹配错误
第二个
VirtualHost
指令是否有问题,该指令会阻止URL被重写?在发出重定向之前,必须验证SSL证书,因此,您需要使用一个对从https://A.org/
,并将其与VirtualHost服务分开https://B.org/
线路检查服务器端口不是必需的,因为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>
服务器名B.org
服务器别名A.org
重新启动发动机
重写cond%{HTTP_HOST}=B.org
重写规则^(.*)$https://B.org$1[R=永久性,L]
服务器名A.org
重新启动发动机
重写规则^(.*)$https://B.org$1[R=永久性,L]
服务器名B.org
Comment outRewriteCond%{SERVER\u PORT}^443(s)
line并在清除浏览器缓存后重新测试。我得到了相同的行为。