Linux – Best practices setting up Apache2 Virtualhosts with Aliases

aliasesapache-2.2debianlinuxvirtualhost

I'm setting my Apache2 Virtual Hosts, in a Debian Squeeze server, in this way:

filename: whatever.mydomain.com

Alias /whatever /var/www/whatever.mydomain.com

<VirtualHost *:80>
        ServerAlias whatever.mydomain.com
        ServerAdmin myemail@mydomain.com
        VirtualDocumentRoot /var/www/%0 

        ErrorLog /var/log/apache2/whatever.mydomain.com-error.log
        CustomLog /var/log/apache2/whatever.mydomain.com-access.log combined
</VirtualHost>

<Directory /var/www/whatever.mydomain.com>
        AuthPAM_Enabled On
        AuthType basic
        AuthName "Authorization Required to proceed"
        AuthBasicAuthoritative off
        AuthUserFile /dev/null
        Require valid-user
</Directory>

I would like to serve the same site with two URL's:
whatever.mydomain.com and www.mydomain.com/whatever

So to help this issue I've configured the Alias /whatever /var/www/whatever.mydomain.com in the start of file. But I'm not aware if this is a good practice. And about the VirtualHost setting? There's something that can be done better?

And last question: there's a way to use variables inside of the file? As example repeat the %0 in other places than VirtualDocumentRoot?

Thanks in advance,

EDIT:
I've got a new problem. With the global alias, all my virtualhosts will answer to the /whatever alias, and I only want the alias being applied on www.mydomain.com. I'm struggling with RedirectMatch without success at this moment. I'm using redirect instead of alias because with alias the access and error log don't get in the virtualhost log file.

Best Answer

You might want to put the Alias in it's own virtual host. For example:

<VirtualHost *:80>
    ServerName www.mydomain.com
    ServerAdmin myemail@mydomain.com
    DocumentRoot /var/www/whatever.mydomain.com

    Alias /whatever /var/www/whatever.mydomain.com
    ErrorLog /var/log/apache2/www.mydomain.com-error.log
    CustomLog /var/log/apache2/www.mydomain.com-access.log combined
</VirtualHost>

That is how I would do it, but of course there are tons of ways do do things properly in Apache. If it works, it works...

As to your last question: You can't really use variables in config files (apart from a few exceptions), but there are third party modules that might be worth investigating. One is mod_macro: http://people.apache.org/~fabien/mod_macro/ Another one that can be used to make config files more dynamic is mod_perl, but that is a really advanced topic.

Related Topic