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

My Apache Virtual Hosts config currently has several repeats of this;

<VirtualHost *:80>
    ServerName example1.com
    ServerAlias www.example1.com

    DocumentRoot /home/example1.com/www

    LogLevel warn
    ErrorLog /home/example1.com/logs/error.log
    CustomLog /home/example1.com/logs/access.log combined

    Alias /dev /home/example1.com/dev
    ScriptAlias /cgi-bin/ /home/example1.com/cgi-bin
</VirtualHost>

How would one go about changing that so all the 'example1.com' instances are replaced with whatever domain the user is currently viewing?

I know of mod_vhost_alias, but I don't see any mention of it being able to do logs or alias (aside from ScriptAlias) dynamically as well. Is there some way to accomplish that? I'd rather not sacrifice separate logs for this.

share|improve this question

3 Answers 3

up vote 1 down vote accepted

You could probably solve the alias with a mod_rewrite rule:

# get the server name from the Host: header
UseCanonicalName Off

# remove www.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ 
RewriteRule .? - [E=noWWWHost:%1]

# map /dev  to the documentroot for dev
RewriteCond %{REQUEST_URI} !^/~
RewriteCond %{HTTP_HOST} ^www\. 
RewriteRule ^/dev/(.*)$ /home/${lowercase:%{ENV:noWWWHost}}/dev/$1


# not using www
RewriteCond %{REQUEST_URI} !^/~
RewriteCond %{HTTP_HOST} !^www\. 
RewriteRule ^/dev/(.*)$ /home/${lowercase:%{HTTP_HOST}}/dev/$1


# make a log that is "splitable" on host
LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
CustomLog logs/access_log vcommon

Edit: Forgot to add that the config above is untested :)

share|improve this answer

There are a couple of ways to go about this ... you can create a virtual host with a related log file for every host. If you are looking for wild card matching it may be easier just to parse the logs later with the given hostname.

share|improve this answer
    
What about Alias? I know there is VirtualScriptAlias, but I don't see any Alias equivalent. –  Stephen Belanger Sep 30 '09 at 3:17

You can use mod_vhost_alias and change the log format by adding %V (Effective server name) at the beginning of the line.

Then you just have to write a simple shell script or perl program that will parse the raw log and write it to the relevant log file. You can even do it in real time using a piped log: CustomLog "|/path/to/script.pl" combined_vhost

share|improve this answer

Your Answer

 
discard

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.