1

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:

  1. 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;
  2. all the other tlds to redirect via a 301 redirect to https://www.policymakr.com which is hosted separately by a managed WordPress host;
  3. 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 webmaster@policymakr.com
   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 webmaster@policymakr.com
   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.

  • Your <VirtualHost *:80> for policymakr.domains redirects to http (not https) - is that intentional? FWIW, just because the site is "hosted on a shared server", doesn't necessarily mean you wouldn't be able to perform the necessary redirects there (although you would no doubt be restricted to .htaccess). – MrWhite Dec 30 '18 at 22:49
  • Yes, I want to use a vhost rather than .htaccess. The advantage of that method is I can use a single server to configure several dozen .conf files, not all hosted at the same ips. And, yes, the http instead of https in the first virtual host was a typo, which I corrected after posting this here. – Donald Jenkins Dec 31 '18 at 0:11
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.