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
.
I am using the following directives:
<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>
When I visit http://A.org/foo
, this redirects to https://B.org/foo
(correct).
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.
Is there something wrong with the second VirtualHost
directive which would keep the URL from being rewritten?
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 servinghttps://B.org/
.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!