Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 将http和https请求重定向到新主机_Apache_.htaccess_Redirect_Ssl - Fatal编程技术网

Apache 将http和https请求重定向到新主机

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

在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://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 out
RewriteCond%{SERVER\u PORT}^443(s)
line并在清除浏览器缓存后重新测试。我得到了相同的行为。