How to redirect old HTTPS URLs to new location HTTPS, on Apache

apache-2.2apache-2.4

I've got many old addresses and servers, https://site1.example.com, https://foo.example.com and so on. I want to stop having so many servers and SSLs certs and move everything to one server and domain. The old links need to remain valid.

So far I've moved the web apps to a new server, https://apps.example.com/name, https://apps.example.com/foo etc.. which work fine when those addresses are used.

I want to switch off the old servers which have the Apache (2.2) redirects:

NameVirtualHost 123.123.123.123:80
NameVirtualHost 123.123.123.123:443
<VirtualHost 123.123.123.123:80>
Servername site1.example.com
redirect permanent / https://apps.example.com/site1
</VirtualHost>
<VirtualHost 123.123.123.123:443>
Servername site1.example.com
redirect permanent / https://apps.example.com/site1
</VirtualHost>
<VirtualHost 123.123.123.123:80>
Servername site2.example.com
redirect permanent / https://apps.example.com/site2
</VirtualHost>
...

The above works, but it means I need to keep this old hardware server switched on.

As a test I've pointed the site1.example.com to the IP address of apps.example.com (the new server) and in the Apache (2.4) conf, I have:

NameVirtualHost 234.234.234.234:80
NameVirtualHost 234.234.234.234:443
<VirtualHost 234.234.234.234:80>
Servername site1.example.com
redirect permanent / https://apps.example.com/site1
</VirtualHost>
<VirtualHost 234.234.234.234:443>
Servername site1.example.com
redirect permanent / https://apps.example.com/site1
</VirtualHost>

# Main config
<VirtualHost *:80>
Servername apps.example.com
redirect permanent / https://apps.example.com/name
</VirtualHost>
<VirtualHost *:443>
Servername apps.example.com
alias /site1 /srv/www/site1
alias /site2 /srv/www/site2
...
</VirtualHost>

http://site1.example.com just goes to the root https://apps.example.com not apps.example.com/site1.
https://site1.example.com gives an error, SSL not valid, hostname apps.example.com doesn't match etc. Any help greatly appreciated.

Best Answer

This is a guess, but I would recommend against mixing NameVirtualHost directives with IP addresses and asterisks. My advice is to try converting everything to asterisks and use ServerName and ServerAlias directives to determine which legacy domain the client is requesting, and based on that, implement the appropriate redirect... in other words:

NameVirtualHost *:80
NameVirtualHost *:443
...
<VirtualHost *:80>
Servername site1.example.com
redirect permanent / https://apps.example.com/site1
</VirtualHost>
<VirtualHost *:443>
Servername site1.example.com
redirect permanent / https://apps.example.com/site1
</VirtualHost>
<VirtualHost *:80>
Servername site2.example.com
redirect permanent / https://apps.example.com/site2
</VirtualHost>
<VirtualHost *:443>
Servername site2.example.com
redirect permanent / https://apps.example.com/site2
</VirtualHost>
...
# Main config
<VirtualHost *:80>
Servername apps.example.com
redirect permanent / https://apps.example.com/name
</VirtualHost>
<VirtualHost *:443>
Servername apps.example.com
alias /site1 /srv/www/site1
alias /site2 /srv/www/site2
...
</VirtualHost>

The above ignores SSL cert configuration. Assuming all your domains are example.com (i.e. same domain), it would be beneficial to invest in a wildcard SSL cert to keep things simple.

Related Topic