Apache Rewrite:目录树到子域目录

发布于 2024-08-24 01:39:03 字数 625 浏览 4 评论 0原文

我有一个 Web 应用程序,其中有一组由 50 多个客户端使用的文件,每个站点的所有配置都来自其各自目录中的 config.php 文件。这是通过 PHP 解析 URL 来完成的。所有这些工作正常,只是客户端可以执行的自定义上传文档存在问题,并且位于

/var/www/sites/user1/cache

中可以有多个子目录。所以请求的时候 http://user1.site.com/cache/subdir1/image.jpg 它需要读取 /var/www/sites/user1/cache/subdir1/image.jpg

允许客户端上传任何文件类型,因此我只需要重写即可接受任何 /cache 请求,然后获取子域并指向正确的目录。

想出了这个,但我仍然得到一个无效的页面

RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^\.]+)\.site\.com$
RewriteRule ^cache/(.*)$ /sites/%1/cache/$1 [L]

任何帮助表示赞赏。

I have a web application that has one set of files used by 50+ clients and all the configuration for each site comes from a config.php file in their respective directories. This is accomplished with PHP parsing the URL. All this works fine, just having an issue with custom uploaded documents the client can do and are located in

/var/www/sites/user1/cache

There can be multiple subdirs. So when requesting
http://user1.site.com/cache/subdir1/image.jpg
it needs to be read from
/var/www/sites/user1/cache/subdir1/image.jpg

The client is allowed to upload any file type, so I just need the rewrite to take any /cache requests, then grab the subdomain and point to proper directory.

Came up with this, but am still getting an invalid page

RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^\.]+)\.site\.com$
RewriteRule ^cache/(.*)$ /sites/%1/cache/$1 [L]

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

俯瞰星空 2024-08-31 01:39:03

如果我正确阅读了 RewriteRule 文档,则 L 标志会打开它自己会生成内部重定向,这意味着替换将被解释为本地文件系统路径。

尝试使用完整路径:

RewriteRule ^cache/(.*)$ /var/www/sites/%1/cache/$1 [L]

或执行外部重定向(使用 HTTP 返回状态“302 MOVED TEMPORARILY”),让用户浏览器使用新路径重新发送请求:

RewriteRule ^cache/(.*)$ /sites/%1/cache/$1 [L ,R]

If I read the RewriteRule documentation correctly, the L flag on its own would generate an internal redirection, meaning that the substitution would be interpreted as a local file system path.

Try using the complete path:

RewriteRule ^cache/(.*)$ /var/www/sites/%1/cache/$1 [L]

or do an external redirection (using HTTP return status "302 MOVED TEMPORARILY"), to let the user's browser re-send the request with the new path:

RewriteRule ^cache/(.*)$ /sites/%1/cache/$1 [L,R]

夜吻♂芭芘 2024-08-31 01:39:03

/var/www/ 是文件在文件系统上的位置。我是根据文档根目录进行路由的,因此不需要将其放在那里。但我意识到我错过了 /cache/ 上的前导斜杠。虽然你的答案并不是我真正想要的,但它让我明白了我错过了什么。谢谢。

RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^\.]+)\.site\.com$
RewriteRule ^/cache/(.*)$ /sites/%1/cache/$1 [L]

The /var/www/ is where the files are on the filesystem. I was routing based on the document root so I didn't need to put that there. But I realized I was missing the leading forward slash on the /cache/. Though your answer wasn't really what I was looking for, it made me see what I was missing. Thanks.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^\.]+)\.site\.com$
RewriteRule ^/cache/(.*)$ /sites/%1/cache/$1 [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文