VirtualDocumentRoot处理不存在的文件夹 [英] VirtualDocumentRoot handling non-existent folders

查看:226
本文介绍了VirtualDocumentRoot处理不存在的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Apache,我已经设置了VirtualDocumentRoot和它的工作原理是这样的:

Using Apache, I have set up the VirtualDocumentRoot and it works this way:

<VirtualHost *:80>
    UseCanonicalName Off
    ServerName app.example.com
    ServerAlias *.example.com
    VirtualDocumentRoot "/home/domains/example.com/%1"
    <Directory "/home/domains/example.com/*">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order Allow,Deny
        Allow from all
    </Directory>
</VirtualHost>

如果我把 http://mail.example.com 的URL,该文档根变得 /home/domains/example.com/邮件,如果是 http://www.example.com ,该文档根变得 /家庭/域/例子.COM / WWW ,等等。

If I put the URL of http://mail.example.com, the DocRoot becomes /home/domains/example.com/mail, if it is http://www.example.com, the DocRoot becomes /home/domains/example.com/www, and so on.

现在,我想检查文件夹,如果它存在,当用户请求如 http://myusername.example.com 域一样,在文档根检查 /home/domains/example.com/myusername ,它不存在,所以它应该是指 /home/domains/example.com/ 404 。这可能吗?

Now I would like to check for the folder if it exists, like when the user requests a domain like http://myusername.example.com, the DocRoot checks /home/domains/example.com/myusername and it doesn't exist, so it should refer to /home/domains/example.com/404. Is this possible?

我已经提到了以下问题,但没有很好的:

I have referred the following questions but no good:

  • VirtualDocumentRoot Domains and Subdomains
  • Apache VirtualDocumentRoot redirect non-existing domains

推荐答案

如果您有映射到的文件夹,然后不能你刚才检查文档根目录是否存在等虚拟子域?如果不存在重定向到 404子域这反过来应该使用 /home/domains/example.com/404 也许喜欢这个。

If you have virtual subdomains that are mapped to folders then couldn't you just check for the existance of the document root? If not exists redirect to the 404 subdomain which in turn should use /home/domains/example.com/404 perhaps like this.

<VirtualHost *:80>
    UseCanonicalName Off
    ServerName app.example.com
    ServerAlias *.example.com
    VirtualDocumentRoot "/home/domains/example.com/%1"
    <Directory "/home/domains/example.com/*">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order Allow,Deny
        Allow from all
        RewriteEngine On
        RewriteCond %{DOCUMENT_ROOT} !-d
        RewriteRule ^(.*) http://404.example.com/ [R=301,L]
    </Directory>
</VirtualHost>

或者,如果完全virtualdocumentroot路径没有被拾起%{DOCUMENT_ROOT} 变量,因为动态分配的,也许通过assiging子到ENV这种方式尝试变量然后用它来检查路径。

Or if the full virtualdocumentroot path is not being picked up by %{DOCUMENT_ROOT} variable because of the dynamic assignment, maybe try it this way by assiging subdomain to a env variable then use it to check the path.

  <VirtualHost *:80>
        UseCanonicalName Off
        ServerName app.example.com
        ServerAlias *.example.com
        VirtualDocumentRoot "/home/domains/example.com/%1"
        <Directory "/home/domains/example.com/*">
            Options Indexes FollowSymLinks
            AllowOverride All
            Order Allow,Deny
            Allow from all
            RewriteEngine On
            RewriteCond %{HTTP_HOST} ^(.+)\.example\.com [NC]
            RewriteRule ^(.*)$ - [E=virt_path:%1,N,NS]

            RewriteCond %{DOCUMENT_ROOT}/%{ENV:virt_path} !-d
            RewriteRule ^(.*) http://404.example.com/ [R=301,L]
        </Directory>
    </VirtualHost>

这篇关于VirtualDocumentRoot处理不存在的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆