使用不同的DocumentRoot目录的httpd重写规则

3
有没有一种方法可以创建一个重写规则,使文件位于不同于DocumentRoot路径的位置?例如,我有一个域名http://test.ldt/,其DocumentRoot为/home/test_ltd/,当在static.子域名下请求文件(http://stats.test.ldt/)时,希望查找来自另一个路径的请求文件,例如/home/static_files/。
我被建议使用mod_alias。但是,我不确定如何在需要子域名时使其工作。
对cristis说:
你不对。例如,如果这些是我的httpd规则:
ServerName domain.ltd
ServerAlias www.domain.ltd

DocumentRoot /home/domain_ltd

RewriteEngine on

RewriteCond %{HTTP_HOST} static2.domain.ltd
RewriteRule (.*)$ /home/static_files/$1 [L]

DirectoryIndex index.html index.htm index.php

当客户端请求 static2.domain.ltd/foo.txt 时,文件将在 /home/domain_ltd/home/static_files/$1 中查找。

你是否可以访问httpd.conf文件,还是只能使用.htaccess文件? - Tim Stone
2个回答

1
在我看来,最合理的方法就是将子域名定义为一个具有DocumentRoot指向所需位置的VirtualHost(尽管从技术上讲我们现在进入了ServerFault领域...)。

e.g.:

<VirtualHost *:80>
    ServerName static.domain.tld

    DocumentRoot /home/static_files/

    <Directory /home/static_files>
        Options Indexes FollowSymLinks Includes
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

但是如果你想的话,你也可以通过mod_aliasmod_rewrite的组合来实现。类似这样的代码应该可以工作...

httpd.conf中:

Alias /static /home/static_files

.htaccess 中(或者更好的方式是在 httpd.conf 中的 Directory 部分中针对 /home/domain_tld):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^static
RewriteRule ^!static - [C]
RewriteRule ^(.*)$ static/$1 [PT]

那对我没用。嗯,对我来说似乎没问题。也许只有RR应该是/static/,但其他一切都应该没问题。不知道,不起作用。 - Gajus
没有重写规则,如果您访问子域名,可以轻松访问/home/domain_tld中的内容,对吧?另外,您的服务器日志记录了测试请求失败的情况吗? - Tim Stone

1

1
无法在评论部分阅读;已移至原始帖子下的“to cristis:”处。 - Gajus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接