简体   繁体   English

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

[英]Redirect non-existent dynamic virtual host folders with Apache

Is it possible to redirect to a certain URL if the directory for the virtual host doesn't exist? 如果虚拟主机的目录不存在,是否可以重定向到某个URL? I have a VirtualHost setup below that lets me host a domain just by creating the folder, and pointing DNS to my server. 我在下面有一个VirtualHost设置,可以通过创建文件夹并将DNS指向我的服务器来托管域。

Here is my configuration for the VirtualHost 这是我对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>

You can manage it using rewriting rules. 您可以使用重写规则进行管理。

Just after you rewriting rule to skip www : 在您重写规则以跳过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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM