I got a virtual host configuration file I cannot edit or modify directly, since it's generated by our hosting platform, Aegir. But it does have Include pre.d
and Include post.d
statements that are included before and after the vhost-configuration.
What I'd like to do, is to add vhost-specific rewrite-rules in separate files, so they can automatically get migrated when we add new servers to the clusters.
So in pre.d
I create a file called ServerName_redirects.conf
, where I put the following:
<VirtualHost *:80>
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^servername.com$ [NC]
RewriteRule ^/*(.*)$ https://servername.com%{REQUEST_URI} [L,R=301]
</VirtualHost>
This is just one of many rules that will be used during migration of the site to a new platform. But it doesn't work, until I wrap it in a <VirtualHost servername.com:80>
-block. But since VirtualHost-blocks does not inherit DocumentRoot
and ServerName
from the original vhost's configuration, all URL's will just end up as 404's in the end, unless they redirect traffic to an external site.
And I cannot simply add DocumentRoot
or ServerName
to the ServerName_redirects.conf-file, since these are "dynamic", and can change at any point in time when we move the site between servers or platforms in Aegir.
Although a .htaccess
in every DocumentRoot
would solve the problem, I can't use that either since it's a Drupal multi-site setup, and I'd would have to put every site's rewrite rule in the same .htaccess
file with a RequestCond %{HTTP_HOST} ^www.site-1.com$
for each rule, which isn't especially elegant and I'm worried how it would affect the performance.