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

发布于 2024-09-07 04:46:50 字数 774 浏览 1 评论 0原文

有没有办法对位于不同于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿嘿嘿 2024-09-14 04:46:50

对我来说,最有意义的是将子域定义为一个 VirtualHost,它有一个 DocumentRoot 指向您想要的位置(尽管我们正在转向 ServerFault)从技术上讲,我猜...)

例如:

<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中) > 在 /home/domain_tldDirectory 部分中):

RewriteEngine On
RewriteCond %{HTTP_HOST} ^static
RewriteRule ^!static - [C]
RewriteRule ^(.*)$ static/$1 [PT]

To me, it would make the most sense to just define the subdomain as a VirtualHost that has a DocumentRoot that points where you wanted it to (Although we're moving into ServerFault territory here technically, I guess...)

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>

But you could do it with a combination of mod_alias and mod_rewrite too, if you wanted. Something like this should work...

In httpd.conf:

Alias /static /home/static_files

In .htaccess (or preferably in httpd.conf in the Directory section for /home/domain_tld):

RewriteEngine On
RewriteCond %{HTTP_HOST} ^static
RewriteRule ^!static - [C]
RewriteRule ^(.*)$ static/$1 [PT]
呢古 2024-09-14 04:46:50

您可以在 RewriteRule 语句中包含完整路径。例如:(

RewriteEngine on
RewriteRule   ^/~(([a-z])[a-z0-9]+)(.*)  /home/$2/$1/.www$3

取自 http://httpd.apache.org/docs/ 2.0/misc/rewriteguide.html)

You can include the full path in the RewriteRule statement. For instance:

RewriteEngine on
RewriteRule   ^/~(([a-z])[a-z0-9]+)(.*)  /home/$2/$1/.www$3

(taken from http://httpd.apache.org/docs/2.0/misc/rewriteguide.html)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文