使用别名设置Apache2虚拟主机的最佳实践?

我正在用Debian Squeeze服务器设置我的Apache2虚拟主机

文件名:whatever.mydomain.com

Alias /whatever /var/www/whatever.mydomain.com <VirtualHost *:80> ServerAlias whatever.mydomain.com ServerAdmin [email protected] 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> 

我想用两个url:whatever.mydomain.com和www.mydomain.com/whatever来为同一个网站提供服务

所以为了解决这个问题,我在文件的开始部分configuration了Alias / whatever /var/www/whatever.mydomain.com。 但我不知道这是否是一个好的做法。 关于VirtualHost设置? 有什么可以做得更好?

最后一个问题:有一种方法来使用文件内的variables? 例如在VirtualDocumentRoot之外的地方重复%0?

提前致谢,

编辑:我有一个新的问题。 随着全球别名,我所有的虚拟主机将回答/任何别名,我只希望在www.mydomain.com上应用别名。 我现在正在努力与RedirectMatch没有成功。 我正在使用redirect而不是别名,因为使用别名,访问和错误日​​志不会进入虚拟主机日志文件。

您可能希望将别名放在自己的虚拟主机中。 例如:

 <VirtualHost *:80> ServerName www.mydomain.com ServerAdmin [email protected] 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> 

我就是这样做的,但是当然在Apache中有很多方法可以正确地做事情。 如果它能正常工作,那么…

至于你的最后一个问题:你不能真正在configuration文件中使用variables(除了一些例外),但是第三方模块可能是值得研究的。 一个是mod_macro: http ://people.apache.org/~fabien/mod_macro/另一个可以用来使configuration文件更加dynamic的是mod_perl,但这是一个非常高级的话题。