Score:1

Consolidating many VirtualHosts into one block, is this a job for SetEnvIf?

dj flag

Edit I think what I need here is SetEnvIf

I'm attempting to consolidate all of my port-80 VirtualHosts into one block for convenience, but I'm struggling with the final piece of the puzzle.

All port 80 vhosts accept a www and non-www connection to /.well-known/acme-challenge/. All other requests 301 to https://example.com/ - no www.

I think I need to turn %{HTTP_HOST} into a Defineable variable minus the possible "www", e.g. ${SITE}.

Or maybe there's a better way?

This is where I'm at so far:

<VirtualHost ${IP1}:80>
    Define SITE hmmm
    ServerName example-1.com
    ServerAlias www.example-1.com
    ServerAlias example-2.com
    ServerAlias www.example-2.com
    # And so on... 25 vhosts
    DocumentRoot /home/user/htdocs/${SITE}

    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^/\.well\-known/acme\-challenge/
    RewriteRule ^ https://${SITE}%{REQUEST_URI} [NE,R=301,L]

    <Directory /home/user/htdocs/${SITE}>
        Options FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>
Score:1
kz flag

I think what I need here is SetEnvIf

But, AFAIK, you cannot use environment variables in the DocumentRoot and <Directory> directives. (And you can't Define variables to a calculated/dynamic string using a regex.)

DocumentRoot /home/user/htdocs/${SITE}

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/\.well\-known/acme\-challenge/
RewriteRule ^ https://${SITE}%{REQUEST_URI} [NE,R=301,L]

<Directory /home/user/htdocs/${SITE}>

You could instead define a VirtualDocumentRoot and use an appropriate regex in the mod_rewrite directives and <DirectoryMatch> container.

For example, assuming all your domains are of the form <name>.<tld> then try the following instead of the above subset of directives:

VirtualDocumentRoot "/home/user/htdocs/%-2.0.%-1.0"

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+?)\.?$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,R=301,L]

<DirectoryMatch "/home/user/htdocs/[^.]+\.com">

The %-2.0.%-1.0 part of the VirtualDocumentRoot directive captures the domain name and TLD (the last 2 parts) from the requested hostname. Importantly, this excludes an optional www subdomain (3rd part from the right).

The %1 backreference in the RewriteRule substitution string references the part of the hostname less the optional www. prefix (subdomain) captured in the preceding CondPattern. (This also excludes the trailing dot in the case of a FQDN.)

The <DirectoryMatch> directive allows access to any directory that is of the form /home/user/htdocs/<example>.com.

Reference:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.