Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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} ^$ [NC]
    RewriteRule ^/*(.*)${REQUEST_URI} [L,R=301]

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>-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} ^$ for each rule, which isn't especially elegant and I'm worried how it would affect the performance.

share|improve this question
why don't use a .htaccess file ? – silviud Apr 26 '11 at 13:14
GregD: It's not a duplicate, because that article does only cover the syntax/usage of mod_rewrite, and this is primarily a Apache2.2 configuration question. – jgabor Apr 26 '11 at 13:56
silviud: I can't use a .htaccess-file, because that's also generated by the hosting-platform. – jgabor Apr 26 '11 at 13:58

Without knowing the details of your hosting environment, I can't give you a definite answer to your question. However, I am thinking you might find a solution to your problem on Apache's Mass Virtual Hosting page. If I am understanding your problem correctly, and I hope I am, you might be able to do something similar to:

<VirtualHost *:80>
    RewriteEngine on
    RewriteCond %{HTTPS} !=on
    RewriteRule ^/*(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    VirtualDocumentRoot /www/hosts/%0/docs
    VirtualScriptAlias /www/hosts/%0/cgi-bin 

You'd have to tweak your VirtualDocumentRoot and VirtualScriptAlias lines to fit your environment, but the details for doing so are in the Apache docs for mod_vhost_alias. Again, I'm hoping I am understanding your problem correctly, and I hope this helps!

share|improve this answer
Unfortunately, no. That didn't solve the problem, but it was a good try... The problem is that the site get's moved between directories because of platforms/codebases, so when doing a migration of a new version of the site it get's moved from /www/platform-1.0/sites/ to /www/platform-1.1/sites/ – jgabor Apr 28 '11 at 11:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.