I have to define about 100 subdomain virtualhost entries in server config file . I want to know if there is any way to do it with regular expression to increase codes in apache config file?

2 Answers 2


You should have a look at this how to from the Apache documentation, I think that's what you need here:

The basic idea is to replace all of the static configuration with a mechanism that works it out dynamically. This has a number of advantages:

  • Your configuration file is smaller so Apache starts faster and uses less memory.

  • Adding virtual hosts is simply a matter of creating the appropriate directories in the filesystem and entries in the DNS - you don't need to reconfigure or restart Apache.


Using Mass Virtual Hosting forbids the use of different log files:

The main disadvantage is that you cannot have a different log file for each virtual host; however if you have very many virtual hosts then doing this is dubious anyway because it eats file descriptors. It is better to log to a pipe or a fifo and arrange for the process at the other end to distribute the logs to the customers (it can also accumulate statistics, etc.).

However this is not really a problem because you can use the %v filter for CustomLog which places the ServerName in front of each logged request, you can then filter and split logs if necessary based on this.

A good practice is to put those logs in a mysql database (on the fly or through a cronjob, pipe or a fifo file) because SQL is a great tool to extract meaningful informations from them.
Have a look on this article which describes settign up (direct) database logging through mod_log_mysql:

Storing your logs directly in a database mysql can be a good solution through the ARCHIVE storage engine but it has its own limitations (lock table on write). An alternative would be building you system on a more standard storage engine (innodb, xtradb for example), if you already have a mysql server I'd try that before other solutions as it doesn't require much learning.

If you feel a little more adventurous you can have a look at MongoDB, as a document oriented database it is particularly fit as a log storage system (fast, powerful filter capabilities, speed of insertion/read) but be sure to have a good dedicated server for it as you'll need RAM for it to behave adequately.

You'll find details on MongoDB logging here:

  • But it is said in the documentation that it doesn't support different log file for each virtualhost . but i need this feature .
    – hd01
    Dec 6, 2011 at 8:51
  • Then I can say you've got a problem :) Anyway you can always put those logs in a database, sorting them by vhost using the "%v" filter for you CustomLogs. You can even transfer those logs with a script if you don't want apache logging directly to your mysql database. I'll add some links about that in my answer above.
    – Shadok
    Dec 6, 2011 at 10:03
  • Since apache allows you to send logs anywhere you want (using a pipe), this is not a real problem. Simply pipe the log into a processor that splits up the vhosts to different files; if you prepend the logs with %v, or even {HTTP_HOST}i, this is easy to do with awk(1). However, much will depend on volume - if this is a million-hits-per-day host, a mysql database may fulfill your needs, at considerable performance cost, but if it hits 1 million per hour, storing logs in a database is counter-productive.
    – adaptr
    Dec 6, 2011 at 10:57

No, but what are you trying to do? Having a regex in the <VirtualHost> line would make no sense.

ServerAlias can have wildcards, not regex; this might give you what you need?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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