0

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.

3 Answers3

2

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 :)

rkthkr
  • 8,618
  • 28
  • 38
0

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

Julien
  • 510
  • 1
  • 5
  • 11
-1

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.

trent
  • 3,114
  • 19
  • 17