virtual host force https and redirect www to non-www, but no other subdomains
<p>This is essentially the same question as <a href="https://stackoverflow.com/q/21467329/1468130">htaccess force https and redirect www to non-www, but no other subdomains</a> (i.e., I want to configure Apache to redirect all non-HTTPS <em>and/or</em> "www" URLs to HTTPS non-www URLs), but I want to configure Apache via a Virtual Host rather than an <code>.htaccess</code> file (since I read that avoiding <code>.htaccess</code> has some benefits).</p>

<p>I was able to get the following answer to work when using an <code>.htaccess</code> file: <a href="https://stackoverflow.com/a/34333450/1468130">https://stackoverflow.com/a/34333450/1468130</a> But it did not work when I tried transferring that answer's configuration to my Virtual Hosts configuration; "<a href="https://www.domain.com" rel="nofollow noreferrer">https://www.domain.com</a>" never redirected to "<a href="https://domain.com" rel="nofollow noreferrer">https://domain.com</a>".</p>

<p>I read up on the differences between <code>.htaccess</code> and Virtual Host <code>.conf</code> files, and found this <a href="http://tltech.com/info/rewriterule-in-htaccess-vs-httpd-conf/" rel="nofollow noreferrer">http://tltech.com/info/rewriterule-in-htaccess-vs-httpd-conf/</a> and this: <a href="https://www.digitalocean.com/community/questions/can-you-use-virtual-host-config-conf-to-redirect-www-domain-to-non-www?answer=15129" rel="nofollow noreferrer">https://www.digitalocean.com/community/questions/can-you-use-virtual-host-config-conf-to-redirect-www-domain-to-non-www?answer=15129</a> which seemed to hint that I could just wrap the configuration in a <code>&lt;Directory&gt;</code> block and it would work. Unfortunately, it doesn't ("<a href="https://www.domain.com" rel="nofollow noreferrer">https://www.domain.com</a>" is still never redirected to "<a href="https://domain.com" rel="nofollow noreferrer">https://domain.com</a>"), so I'm wondering if the Internet knew what I was doing wrong:</p>

<pre><code>&lt;VirtualHost *:80&gt;
        ServerName domain.com
        ServerAlias www.domain.com

        ServerAdmin admin@domain.com
        DocumentRoot /var/www/domain.com/

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        &lt;Directory /var/www/domain.com/&gt;
                RewriteEngine On

                # match any URL with www and rewrite it to https without the www
                RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
                RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]

                # match urls that are non https (without the www)
                RewriteCond %{HTTPS} off
                RewriteCond %{HTTP_HOST} !^(www\.)(.*) [NC]
                RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
        &lt;/Directory&gt;
&lt;/VirtualHost&gt;
</code></pre>

<p>I've also tried configuring <code>&lt;VirtualHost *:443&gt;</code> as Dusan Bajic suggested in the comments, but that has no effect either; <code>https://www.domain.com</code> still won't redirect to <code>https://domain.com</code>:</p>

<pre><code>&lt;VirtualHost *:443&gt;
        ServerName domain.com
        ServerAlias www.domain.com

        ServerAdmin admin@domain.com
        DocumentRoot /var/www/domain.com/

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        SSLCertificateFile /etc/letsencrypt/live/domain.com/cert.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem
        Include /etc/letsencrypt/options-ssl-apache.conf
        SSLCertificateChainFile /etc/letsencrypt/live/domain.com/chain.pem

        &lt;Directory /var/www/domain.com/&gt;
                RewriteEngine On

                # match any URL with www and rewrite it to https without the www
                RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
                RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]
        &lt;/Directory&gt;
&lt;/VirtualHost&gt;
</code></pre>

<p>Also per the comments, I have tried the above <code>*:443</code> configuration paired with a <code>*:80</code> configuration with the <code>&lt;Directory&gt;</code> block changed to only redirect HTTP to HTTPS. But when I do that, "www" never gets removed.</p>

<pre><code>&lt;Directory /var/www/paradoxmayhem.com/&gt;
        RewriteEngine On

        RewriteCond %{SERVER_NAME} =www.paradoxmayhem.com [OR]
        RewriteCond %{SERVER_NAME} =paradoxmayhem.com
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
&lt;/Directory&gt;
</code></pre>