I have a Ubuntu 18.04 server on an Amazon EC2 instance running Apache 2, with an SSL certificate installed, which I use exclusively to host virtual hosts for .tld variations of the policymakr.com domain, sharing the same fixed IP.
I want the configuration to achieve the following:
- the main domain (policymakr.domains, which has a index.html page and is meant to be accessible on the server by SSL) to be accessible on the server;
- all the other tlds to redirect via a 301 redirect to https://www.policymakr.com which is hosted separately by a managed WordPress host;
- redirect all http inbound policymakr.domains traffic to https, and all policymakr.domains traffic to www.policymakr.domains.
(The reason I'm doing this is because I want to remap the tlds via a 301 for SEO purposes, and this can't be achieved by the host of my WordPress site because the site is hosted on a shared server.)
I started setting up the tlds, and they redirected fine. The .conf files for the tlds (.net, .org, .info and .io) are all identical and look like this:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName www.policymakr.[tld]
DocumentRoot /var/www/html/policymakr.[tld]/
ErrorLog /var/www/logs/error.log
Redirect 301 / https://www.policymakr.com/
</VirtualHost>
After I set up the tld virtual hosts, the 301 Redirects all started working flawlessly.
I kept setting up the virtual host for policymakr.domains to last, owing to the slight extra complexity of needing to install an external SSL certificate (Amazon certificates don't seem to be able to be attached to EC2 instances).
The policymakr.domains virtual host I set up looks like this:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName www.policymakr.domains
ServerAlias policymakr.domains
DocumentRoot /var/www/html/policymakr.domains/
ErrorLog /var/www/logs/error.log
RewriteEngine on
RewriteCond %{HTTP_HOST} ^policymakr\.domains
RewriteRule ^(.*)$ http://www.policymakr.domains/$1 [R=permanent,L]
</VirtualHost>
<VirtualHost *:443>
ServerName www.policymakr.domains
ServerAlias policymakr.domains
DocumentRoot /var/www/html/policymakr.domains/
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/[crt file name]
SSLCertificateKeyFile /etc/apache2/ssl/private/[private key]
SSLCertificateChainFile /etc/apache2/ssl/[bundle file]
RewriteEngine on
RewriteCond %{HTTPS_HOST} ^policymakr\.domains
RewriteRule ^(.*)$ https://www.policymakr.domains/$1 [R=permanent,L]
</VirtualHost>
After this, the redirections for policymakr.domains are functioning exactly as I wanted (i.e., non-www to www, http to https), but the 301 Redirects for the other tlds have stopped working. Instead, they all now redirect to https://www.policymakr.domains.
I basically don't know what I'm getting wrong here.
The whole configuration actually ended up working after a few hours, it might have been some sort of caching issue. The only change I added was a
ServerAlias policymakr.[tld]
entry in each.conf
file.