使用Apache重定向不存在的动态虚拟主机文件夹

时间:2011-12-23 22:50:47

标签: dynamic virtual apache host

如果虚拟主机的目录不存在,是否可以重定向到某个URL?我在下面有一个VirtualHost设置,它允许我通过创建文件夹并将DNS指向我的服务器来托管域。

这是我对VirtualHost的配置

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost

        RewriteEngine On
        RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
        RewriteRule ^(.*) http://%1/ [R=301,L]

        VirtualDocumentRoot /var/www/%0
        <Directory /var/www/%0>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog /var/log/apache2/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/log/apache2/access.log combined
        ServerSignature On

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>
</VirtualHost>

1 个答案:

答案 0 :(得分:1)

您可以使用重写规则进行管理。

在您重写规则后跳过www:

RewriteCond %{HTTP_HOST} ^(.*)$
RewriteCond /var/www/%1 !-d 
# I think just /var/www/%{HTTP_HOST} works :|
RewriteRule .* http://example.com/nonexistant.php/$0 [L,R]