1

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.

4

2 Answers 2

0

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 
</VirtualHost>

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!

1
  • 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/site-1.com to /www/platform-1.1/sites/site-1.com
    – jgabor
    Apr 28, 2011 at 11:18
0

Create a [hook].drush.inc file in /var/aegir/.drush/ to hold a function that will alter the vhost when it's generated. Implement the Drush hook hook_provision_apache_vhost_config($uri, $data) provided by Provision. $uri is the site being verified, same as the site name in Aegir. You can return either a string that should be injected into the vhost or an array of strings. Aegir will invoke that hook whenever it verifies a site.

Unlike my example below, I usually return something like "Include /etc/apache2/example.com.conf" so that I can change the configuration without needing to verify the site; I can just restart Apache.

/**
 * Implements hook_provision_apache_vhost_config().
 *
 * Insert custom vhost configurations into Aegir's template.
 */
function example_provision_apache_vhost_config($uri, $data) {
  if ($uri == 'example.com') {
    return "
  RewriteEngine on
  RewriteCond %{HTTP:X-Forwarded-Proto} ^http$
  RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

";
  }
}

See http://docs.aegirproject.org/en/3.x/extend/altering-behaviours/#injecting-into-site-vhosts.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .