title: Apache Virtual Host Server Alias matching using Redirect
body: <p>Apache 2.4.6 is hosting four domains using name based Virtual Hosts. Calls to port 80 for http are redirected to port 443. Each virtual server has two ServerAlias args for the directive to redirect calls to <a href="http://domain-example.com/" rel="nofollow noreferrer">http://domain-example.com/</a> or <a href="http://www.domain-example.com/" rel="nofollow noreferrer">http://www.domain-example.com/</a> to <a href="https://domain-example.com/" rel="nofollow noreferrer">https://domain-example.com/</a>.</p>
  
  <p>The problem is that the call to <a href="http://example2.com/" rel="nofollow noreferrer">http://example2.com/</a> gets redirected by the default Virtual Host (first in order) <a href="https://example1.com/" rel="nofollow noreferrer">https://example1.com/</a>. Even further head scratching is that <a href="http://example2.com/somepath" rel="nofollow noreferrer">http://example2.com/somepath</a> properly redirects to <a href="https://example2.com/somepath" rel="nofollow noreferrer">https://example2.com/somepath</a>.</p>
  
  <p>Summary of calls &amp; results:</p>
  
  <pre><code>http://example1.com/    ==&gt; https://example1.com/
  http://www.example1.com/    ==&gt; https://example1.com/
  https://example1.com/   ==&gt; https://example1.com/
  https://www.example1.com/   ==&gt; https://example1.com/
  
  http://example2.com/    ==&gt; https://example1.com/ (&lt;== THIS ONE!)
  http://www.example2.com/    ==&gt; https://example2.com/
  https://example2.com/   ==&gt; https://example2.com/
  https://www.example2.com/   ==&gt; https://example2.com/
  
  http://example3.us/ ==&gt; https://example3.us/
  http://www.example3.us/ ==&gt; https://example3.us/
  https://example3.us/    ==&gt; https://example3.us/
  https://www.example3.us/    ==&gt; https://example3.us/
  
  http://example4.com/    ==&gt; https://example4.com/
  http://www.example4.com/    ==&gt; https://example4.com/
  https://example4.com/   ==&gt; https://example4.com/
  https://www.example4.com/   ==&gt; https://example4.com/
  
  http://example2.com/    ==&gt; https://example1.com/
  http://example2.com/somepath    ==&gt; https://example2.com/somepath
  </code></pre>
  
  <p>virtualhost.conf (firewalled ports replaced by [value])</p>
  
  <pre><code>NameVirtualHost *:80
  NameVirtualHost *:443
  SSLStrictSNIVHostCheck off
  
  &lt;VirtualHost *:80&gt;
    Serverexample www.example1.com
    ServerAlias example1.com *.example1.com
    Redirect permanent / https://example1.com/
  &lt;/VirtualHost&gt;
  
  &lt;VirtualHost *:443&gt;
    Serverexample www.example1.com
    ServerAlias example1.com *.example1.com
    ProxyRequests off
    ProxyPreserveHost on
    CustomLog "/path/to/logs/example1ssl.log" "%h %l %u %t \"%r\" %&gt;s %b"
    ErrorLog "/path/to/logs/example1ssl_error.log"
    SSLEngine on
    SSLProxyEngine on
    SSLCertificateFile /path/to/certs/example1.com.crt
    SSLCertificateKeyFile /path/to/private/example1.key
    SSLCertificateChainFile /path/to/certs/ca-bundle-example1.crt
    ProxyPass / http://example1.com:[internal port 1]/
    ProxyPassReverse / http://example1.com:[internal port 1]/
  &lt;/VirtualHost&gt;
  
  &lt;VirtualHost *:80&gt;
    Serverexample www.example2.com
    ServerAlias example2.com *.example2.com
    Redirect permanent / https://example2.com/
  &lt;/VirtualHost&gt;
  
  &lt;VirtualHost *:443&gt;
    Serverexample www.example2.com
    ServerAlias example2.com *.example2.com
    SSLEngine on
    SSLProxyEngine on
    CustomLog "/path/to/logs/example2_ssl.log" "%h %l %u %t \"%r\" %&gt;s %b"
    ErrorLog "/path/to/logs/example2_ssl_error.log"
    SSLCertificateFile /path/to/certs/web-01.example2.com.crt
    SSLCertificateKeyFile /path/to/private/example2.com.key
    SSLCertificateChainFile /path/to/certs/example2.com.crt
    ProxyPreserveHost On
    ProxyRequests off
    ProxyPass / http://example2.com:[internal port 3]/
    ProxyPassReverse / http://example2.com:[internal port 3]/
  &lt;/VirtualHost&gt;
  
  &lt;VirtualHost *:80&gt;
    Serverexample www.example3.us
    ServerAlias example3.us *.example3.us
    Redirect permanent / https://example3.us/
  &lt;/VirtualHost&gt;
  
  &lt;VirtualHost *:443&gt;
    Serverexample www.example3.us
    ServerAlias example3.us *.example3.us
    ProxyRequests off
    ProxyPreserveHost on
    CustomLog "/path/to/logs/example3ssl.log" "%h %l %u %t \"%r\" %&gt;s %b"
    ErrorLog "/path/to/logs/example3ssl_error.log"
    SSLEngine on
    SSLProxyEngine on
    SSLCertificateFile /path/to/certs/example3.us.crt
    SSLCertificateKeyFile /path/to/private/example3.key
    SSLCertificateChainFile /path/to/certs/auth_bundle-example3.crt
    ProxyPass / http://example3.us:[internal port 2]/
    ProxyPassReverse / http://example3.us:[internal port 2]/
  &lt;/VirtualHost&gt;
  
  
  
  &lt;VirtualHost *:80&gt;
    Serverexample www.example4.com
    ServerAlias example4.com *.example4.com
    Redirect permanent / https://example4.com/
  &lt;/VirtualHost&gt;
  
  &lt;VirtualHost *:443&gt;
    Serverexample www.example4.com
    ServerAlias example4.com *.example4.com
    ProxyRequests off
    ProxyPreserveHost on
    CustomLog "/path/to/logs/example4ssl.log" "%h %l %u %t \"%r\" %&gt;s %b"
    ErrorLog "/path/to/logs/example4ssl_error.log"
    SSLEngine on
    SSLProxyEngine on
    SSLCertificateFile /path/to/certs/example4.com.crt
    SSLCertificateKeyFile /path/to/private/example4.key
    SSLCertificateChainFile /path/to/certs/ca-bundle-example4.crt
    ProxyPass / http://example4.com:[internal port 4]/
    ProxyPassReverse / http://example4.com:[internal port 4]/
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>From apachectl -S</p>
  
  <pre><code>VirtualHost configuration:
  
      *:80                   is a NameVirtualHost
               default server www.example1.com (/etc/httpd/conf.d/virtualhosts.conf:12)
               port 80 namevhost www.example1.com (/etc/httpd/conf.d/virtualhosts.conf:12)
                       alias example1.com
                       wild alias *.example1.com
               port 80 namevhost www.example2.com (/etc/httpd/conf.d/virtualhosts.conf:36)
                       alias example2.com
                       wild alias *.example2.com
               port 80 namevhost www.example3.us (/etc/httpd/conf.d/virtualhosts.conf:84)
                       alias example3.us
                       wild alias *.example3.us
               port 80 namevhost www.example4.com (/etc/httpd/conf.d/virtualhosts.conf:108)
                       alias example4.com
                       wild alias *.example4.com
      *:443                  is a NameVirtualHost
               default server www.example1.com (/etc/httpd/conf.d/virtualhosts.conf:19)
               port 443 namevhost www.example1.com (/etc/httpd/conf.d/virtualhosts.conf:19)
                       alias example1.com
                       wild alias *.example1.com
               port 443 namevhost www.example2.com (/etc/httpd/conf.d/virtualhosts.conf:43)
                       alias example2.com
                       wild alias *.example2.com
               port 443 namevhost www.example3.us (/etc/httpd/conf.d/virtualhosts.conf:90)
                       alias example3.us
                       wild alias *.example3.us
               port 443 namevhost www.example4.com (/etc/httpd/conf.d/virtualhosts.conf:114)
                       alias example4.com
                       wild alias *.example4.com
  </code></pre>
  
  <p>Thank you</p>