mod_rewrite 缩短 url 文件路径

发布于 2024-12-15 22:26:43 字数 736 浏览 0 评论 0原文

我在让 mod_rewrite 做我需要它做的事情时遇到了一些困难。

我们在 Drupal 安装中有一组虚拟子域。因此,academics.univ.edu、about.univ.edu 等都是同一核心 Drupal 安装的一部分。

目前的文件访问权限为academics.univ.edu/sites/all/academics/files/myfile.jpg。但是,此路径也可以用作 about.univ.edu/sitse/all/about/files/myfile.jpg 或任何其他有效的子域。

我们想使用 mod_rewrite 接受 academics.univ.edu/files/myfile.jpg 并从上述位置传送文件。

这是我尝试过的:

RewriteCond %{REQUEST_URI} ^(about|academics|bursar|calendar)\.univ\.edu\/files\/(.*)$ [NC]
RewriteRule ^/sites/all/files/$1/$2 [L,NC]

我可能以错误的方式处理这个问题,但我想检查一下。我可以通过使用 HTTP_HOST 制定单独的规则来使子域正常工作,但我希望文件中的规则更少。另外,我无法让 HTTP_HOST 在作为子域名中的子目录存在的网站上工作。例如,undergrad.univ.edu/biology/files/myfile.jpg 应提供 /sites/all/biology/files/myfile.jpg

I am having a bit of difficulty getting mod_rewrite to do what I need it to do.

We have a group of virtual subdomains in a Drupal install. So, academics.univ.edu, about.univ.edu, etc are all part of the same core Drupal install.

File access currently is academics.univ.edu/sites/all/academics/files/myfile.jpg. However this path will also work as about.univ.edu/sitse/all/about/files/myfile.jpg or any other valid subdomain.

We'd like to use mod_rewrite to accept academics.univ.edu/files/myfile.jpg and deliver the file from the above location.

Here's what I've tried:

RewriteCond %{REQUEST_URI} ^(about|academics|bursar|calendar)\.univ\.edu\/files\/(.*)$ [NC]
RewriteRule ^/sites/all/files/$1/$2 [L,NC]

I'm probably going about this the wrong way, but I wanted to check on it. I can get the subdomains to work by making separate rules using HTTP_HOST, but I wanted less rules in the file. Also, I can't get HTTP_HOST to work on sites that exist as a subdirectory in a subdomian. For instance, undergrad.univ.edu/biology/files/myfile.jpg should deliver /sites/all/biology/files/myfile.jpg

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

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

发布评论

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

评论(1

余厌 2024-12-22 22:26:43

您无法匹配 %{REQUEST_URI} 中的主机,需要使用 %{HTTP_HOST},然后使用 %1 反向引用来访问该匹配项。实际的 URI 可以在规则本身中进行匹配。像这样的事情:

RewriteCond %{HTTP_HOST} ^(about|academics|bursar|calendar)\.univ\.edu$  [NC]
RewriteRule ^files/(.*)$ /sites/all/files/%1/%2 [L,NC]

%1 引用 RewriteCond 中的匹配 (about|academics|bursar|calendar) ,而 $1 引用匹配 < RewriteRule 中的 code>(.*)。因此该示例将请求 http://about.univ.edu/files/foo.html 并将请求重写为 /sites/all/files/about/foo.html

另外,如果这是在虚拟主机或服务器配置中,则在重写规则中的“^”和“文件”之间需要有一个“/”。

You can't match a host in the %{REQUEST_URI}, you need to use %{HTTP_HOST}, then use the %1 backrefernce to access that match. The actual URI can be matched in the rule itself. Something like this:

RewriteCond %{HTTP_HOST} ^(about|academics|bursar|calendar)\.univ\.edu$  [NC]
RewriteRule ^files/(.*)$ /sites/all/files/%1/%2 [L,NC]

The %1 references the match (about|academics|bursar|calendar) in the RewriteCond and the $1 references the match (.*) in the RewriteRule. So that example will take a request to http://about.univ.edu/files/foo.html and rewrite the request to /sites/all/files/about/foo.html.

Also, if this is in a virtualhost or server config, you need a "/" in between "^" and "files" in the RewriteRule.

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