Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have a config file for apache2 virtualhosts. For each virtuahost I have a conf file which looks like this:

NameVirtualHost mydomain.co.uk:80

<VirtualHost mydomain.co.uk:80>
        DocumentRoot /home/d/r/drupal/web/public_html
        ServerName mydomain.co.uk
        ServerAlias www.mydomain.co.uk
        ErrorLog /var/log/apache2/sites/mydomain.co.uk_error-log
        CustomLog /var/log/apache2/sites/mydomain.co.uk_access-log "combined"

        # Rewrite the www
        RewriteEngine on
        RewriteCond %{HTTP_HOST} ^www\.mydomain.co.uk/?$ [NC]
        RewriteRule ^(.*)$ http://mydomain.co.uk$1 [L,R=301]

        # Read in the drupal configuration
        Include conf.d/drupal6.conf

        # Block access while developing
#       Include conf.d/beta.conf


How can I specify myDomain in a single place and re-use this string. i.e.

mydomain = "www.SomeDomainIHave.com"

The idea being that I specify the domain one at the top of the file so to avoid any mistakes further down.

Hope this makes sense.


share|improve this question

5 Answers 5

up vote 1 down vote accepted

Alternatively, if most of your vhosts are following similar templates, you could use mod_macro. I've recently set this up for an apache installation with 200+ similar vhosts and it's working like a charm.

You're example would be re-written as such : ( /etc/apache2/macro.d/drupal.vhost.conf (

<Macro drupal0 $domain>
NameVirtualHost $domain:80

<VirtualHost $domain:80>
        DocumentRoot /home/d/r/drupal/web/public_html
        ServerName $domain
        ServerAlias www.$domain
        ErrorLog /var/log/apache2/sites/$domain_error-log
        CustomLog /var/log/apache2/sites/$domain_access-log "combined"

        # Rewrite the www
        RewriteEngine on
        RewriteCond %{HTTP_HOST} ^www\.$domain/?$ [NC]
        RewriteRule ^(.*)$ http://$domain$1 [L,R=301]

        # Read in the drupal configuration
        Include conf.d/drupal6.conf

        # Block access while developing
#       Include conf.d/beta.conf

Using the macro : ( /etc/apache2/sites-available/drupal.sites.conf )

Include /etc/apache2/macro.d/drupal.vhost.conf
Use drupal0 mydomain.co.uk
share|improve this answer
absolutely spot on... this will (and already has) saved on a lot of typos. Thank you. –  belliez Feb 1 '10 at 16:35

Directly you can not. However, if you have amny such parameters that you want to set, then a template-based approach can do. Use m4/sed/ruby/python to substitute variables in your files. Downside is that you must have a process to regenerate the files when you change something but that can be automated.

share|improve this answer
thats a shame.. was hoping for something simple like a variable declaration in c or something! –  belliez Jan 26 '10 at 10:47

have a look at configuration management tools like Puppet or Chef. These will allow you to define configuration file templates and rebuild them when you make any changes to the setting. You then can define generic settings such as domain name and reuse it in your templates. These tools would then deploy new configuration and restart your web services. Just don't reinvent the wheel again writing your own configuration management tool (who hasn't done this yet, btw? :) )

share|improve this answer

You can't do this directly in apache. You'll have to use some other process to generate the apache config files.

share|improve this answer

it is not possible to use variables as Keltia already said, but perhaps mod_vhost_alias can help you with your problem.

  ServerName www.commercial.isp.com

  CustomLog logs/access_log.commercial vcommon

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

%0 in this example will substitute with www.commercial.isp.com.

here are some more examples.

share|improve this answer
I do not think mod_vhot_alias can do that. –  proy Jan 26 '10 at 13:15
i put an example in my answer to show how i think this can work for this problem. –  Christian Jan 28 '10 at 10:49
can I specify www.commercial.isp.com outside of VirtualHost? If you look at the top of my sample script in the question I also include "NameVirtualHost mydomain.co.uk:80" before. –  belliez Jan 29 '10 at 12:38
when you use name based vhosts, you only have to configure NameVirtualHost once for each ip. it is not needed for each virtual host. the doc says you even shouldn't use a fqdn for NameVirtualHost. perhaps it is enough for your config to use NameVirtualHost *. here is the link to the docs: httpd.apache.org/docs/2.0/mod/core.html#namevirtualhost –  Christian Jan 29 '10 at 12:48

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.