title: Apache virtual hosts with multiple domains
body: <p>I am trying to figure out what the optimal configuration is for my virtual host files, but I'm having some issues with it.</p>
  
  <p>Currently I have 2 domains: domain1.com and domain2.com</p>
  
  <p>I have 1 server at IP 000.000.000.001 which actually hosts all the needed files. In these files you have an API (api.domain1.com) and the actual website.</p>
  
  <p>domain1.com is using the nameservers of Digital Ocean and uses the following DNS records:</p>
  
  <pre><code>A        @        000.000.000.001
  CNAME    *        domain1.com.
  </code></pre>
  
  <p>For SEO purposes I want to redirect all requests which are not being made to the api subdomain to forward to www.domain1.com.
  However, I also ONLY want users to be able to browse my site (and API) through a SSL connection, I don't want users being able to use it through HTTP so I try to redirect all those requests to use HTTPS. The certificates are provided by LetsEncrypt!. Their automated install made changes to my virtual hosts files and currently I have 4 of them:</p>
  
  <ol>
  <li><p>api.domain1.com.conf</p>
  
  <pre><code>&lt;VirtualHost *:80&gt;
      ServerName api.domain1.com
  
      DocumentRoot /var/www/api.domain1.com/web
  
      RewriteEngine on
      RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,QSA,R=permanent]
  &lt;/VirtualHost&gt;
  </code></pre></li>
  <li><p>api.domain1.com-le-ssl.conf</p>
  
  <pre><code>&lt;IfModule mod_ssl.c&gt;
  &lt;VirtualHost *:443&gt;
      ServerName api.domain1.com
  
      DocumentRoot /var/www/api.domain1.com/web
  
      ExpiresActive On
      /* ... */
  
      &lt;Directory /var/www/api.domain1.com/web&gt;
          AllowOverride None
          Order Allow,Deny
          Allow from All
  
          &lt;IfModule mod_rewrite.c&gt;
                  Options -MultiViews
                  RewriteEngine On
                  RewriteCond %{REQUEST_FILENAME} !-f
                  RewriteRule ^(.*)$ app.php [QSA,L]
          &lt;/IfModule&gt;
      &lt;/Directory&gt;
  
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined
  
      SSLCertificateFile /etc/letsencrypt/live/domain1.com/cert.pem
      SSLCertificateKeyFile /etc/letsencrypt/live/domain1.com/privkey.pem
      Include /etc/letsencrypt/options-ssl-apache.conf
      SSLCertificateChainFile /etc/letsencrypt/live/domain1.com/chain.pem
  &lt;/VirtualHost&gt;
  &lt;/IfModule&gt;
  </code></pre></li>
  <li><p>domain1.com.conf</p>
  
  <pre><code>&lt;VirtualHost *:80&gt;
      ServerName domain1.com
  
      Redirect 301 / https://www.domain1.com
  &lt;/VirtualHost&gt;
  </code></pre></li>
  <li><p>domain1.com-le-ssl.conf</p>
  
  <pre><code>&lt;IfModule mod_ssl.c&gt;
  &lt;VirtualHost *:443&gt;
      ServerName www.domain1.com
      DocumentRoot /var/www/domain1.com
  
      ExpiresActive On
      /* ... */
  
      RewriteEngine On
      RewriteCond %{HTTP_HOST} ^https://domain1.com
      RewriteRule ^/(.*)$ https://www.domain1.com/$1 [L,R=301]
  
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined
  
      ErrorDocument 404 /404.html
  
      SSLCertificateFile /etc/letsencrypt/live/domain1.com/cert.pem
      SSLCertificateKeyFile /etc/letsencrypt/live/domain1.com/privkey.pem
      Include /etc/letsencrypt/options-ssl-apache.conf
      SSLCertificateChainFile /etc/letsencrypt/live/domain1.com/chain.pem
  &lt;/VirtualHost&gt;
  &lt;/IfModule&gt;
  </code></pre></li>
  </ol>
  
  <p>When I try to visit the site, the following things happen:</p>
  
  <pre><code>    http://test.domain1.com   --&gt;    https://test.domain1.com   FAIL
      http://domain1.com        --&gt;    https://www.domain1.com    SUCCESS
      http://www.domain1.com    --&gt;    https://www.domain1.com    SUCCESS
      https://domain1.com       --&gt;    https://domain1.com        FAIL
      https://www.domain1.com   --&gt;    https://www.domain1.com    SUCCESS
  </code></pre>
  
  <p>As you can see, the first and fourth entry fails. They both return a 404 Not Found to my visitors which is unacceptable to me. The first one even shows a SSL warning (I don't think that LetsEncrypt! supports wildcards so I think this warning cannot be prevented?)</p>
  
  <p><strong>What is causing my test. not redirecting to www.?</strong></p>
  
  <p><strong>What is causing my fourth entry to end up serving a 404?</strong></p>
  
  <p>Now I also want my second domain (domain2.com) to point to this server/files. This domain is hosted at a different location (I got it from a friend) and is using different nameservers. I cannot simply change the nameservers (I think?) because this second domain has email hosting linked to it which uses the nameservers of this other provider. Momentarily, this domain also has its own web server (but this will go away in the future) hosted at 000.000.000.002. Right now, it uses a </p>
  
  <pre><code> &lt;meta http-equiv="refresh" content="0; url=https://www.domain1.com" /&gt;
  </code></pre>
  
  <p>tag in its index.html file to redirect to the right server, in the future this will have to be done in the DNS records though.</p>
  
  <p><strong>How should I do this?</strong></p>
  
  <p>So to summarise:</p>
  
  <ol>
  <li>I thought the CNAME * is a catch-all for all subdomains?</li>
  <li>What did I do wrong in the virtual hosts file? Something is causing that fourth redirect to fail.</li>
  <li>How should I handle my second domain pointing at my first IP?</li>
  <li>Is there another way to only allow HTTPS connections? (This should also be forced on the API virtual host). I've heard of HSTS, should I implement this?</li>
  </ol>
  
  <p>Some SEO tests also pointed out that I need a IP redirect to further improve my SEO results. They give this as an example:</p>
  
  <pre><code>    RewriteCond %{HTTP_HOST} ^000\.000\.000\.001
      RewriteRule (.*) http://www.domain1.com/$1 [R=301,L]
  </code></pre>
  
  <p>Should I also implement this for SEO purposes? Should I also implement this for my other IP address? </p>
  
  <p>So many questions...</p>
  
  <p>Of course, if you have any other suggestions, please share your opinion!</p>