使用不同的 DocumentRoot 目录的 httpd 重写规则
有没有办法对位于不同于 DocumentRoot 路径的文件制定重写规则?假设我有域 http://test.ldt/ 和 DocumentRoot /home/test_ltd/ 我希望在 static. 子域下请求文件时(http://stats .test.ldt/)它会从另一个路径查找请求的文件,比如 /home/static_files/
我被建议使用 mod_alias。但是,我不确定当我需要子域时如何使其工作。
to 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 中搜索该文件
Is there a way to make a rewrite rule to file located in a different than DocumentRoot path? Say I have domain http://test.ldt/ with DocumentRoot /home/test_ltd/ and I want that when a file is requested under static. subdomain (http://stats.test.ldt/) it would look for requested file from another path, say /home/static_files/
I was advised to use mod_alias. However, I am not sure how to make it work when I need it with subdomain.
to cristis:
You are not right. For example if these would be mine httpd rules:
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
And client would request for static2.domain.ltd/foo.txt, the file would be searched in /home/domain_ltd/home/static_files/$1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对我来说,最有意义的是将子域定义为一个
VirtualHost
,它有一个DocumentRoot
指向您想要的位置(尽管我们正在转向 ServerFault)从技术上讲,我猜...)例如:
但是如果您愿意,您也可以使用
mod_alias
和mod_rewrite
的组合来实现。像这样的东西应该工作......在
httpd.conf
中:在
.htaccess
中(或者最好在httpd.conf
中) > 在/home/domain_tld
的Directory
部分中):To me, it would make the most sense to just define the subdomain as a
VirtualHost
that has aDocumentRoot
that points where you wanted it to (Although we're moving into ServerFault territory here technically, I guess...)e.g.:
But you could do it with a combination of
mod_alias
andmod_rewrite
too, if you wanted. Something like this should work...In
httpd.conf
:In
.htaccess
(or preferably inhttpd.conf
in theDirectory
section for/home/domain_tld
):您可以在
RewriteRule
语句中包含完整路径。例如:(取自 http://httpd.apache.org/docs/ 2.0/misc/rewriteguide.html)
You can include the full path in the
RewriteRule
statement. For instance:(taken from http://httpd.apache.org/docs/2.0/misc/rewriteguide.html)