Apache Rewrite:仅重写索引 URL,无论深度如何,但文件扩展名例外

发布于 2024-12-27 11:04:42 字数 741 浏览 2 评论 0原文

尽管理解设置很重要,但我已经有点工作了。

通过多域支持(多个域使用完全相同的软件副本),我只需要重写目录(像 dir1/ 或 dir2/ 一样干净地结束)。

从视觉上看,这里是引用文件的位置...

site1.com/gallery/image.jpg

site2.net/gallery/image.png

gallery/ (此处为 PHP 代码)

.htaccess

所以我只想将目录重写为 gallery/,而所有图像文件(按扩展名)都重写为 site1/gallery/image.jpg 路径。

我目前所拥有的仅重写基本图库/路径,但它不会重写更深的图库索引(例如 gallery/my_gallery/),但图像异常也能正常工作。

RewriteEngine on
RewriteRule ^[^/]*/gallery/$ gallery/$1 [QSA]
RewriteCond %{REQUEST_URI} !\.(gif|jpg|png)$

RewriteCond %{REQUEST_URI} !.*/(gallery|admin|blog)
RewriteRule !\.(css|js)$ index.php

那么如何在保持图像文件异常的同时捕获所有目录(无论深度如何)?

I have this somewhat working though it's important to understand the setup.

With multi-domain support (multiple domains using the exact same copy of software) I need to rewrite ONLY the directories (which end cleanly like dir1/ or dir2/).

Visually here are where the files are referenced...

site1.com/gallery/image.jpg

site2.net/gallery/image.png

gallery/ (PHP code here)

.htaccess

So I want to have only directories rewritten to gallery/ while all image files (by extension) are rewritten to the site1/gallery/image.jpg paths.

What I have currently rewrites ONLY the base gallery/ path however it does NOT rewrite deeper gallery indexes (e.g. gallery/my_gallery/) however the image exception does work as well.

RewriteEngine on
RewriteRule ^[^/]*/gallery/$ gallery/$1 [QSA]
RewriteCond %{REQUEST_URI} !\.(gif|jpg|png)$

RewriteCond %{REQUEST_URI} !.*/(gallery|admin|blog)
RewriteRule !\.(css|js)$ index.php

So how to I capture all directories regardless of depth while maintaining the image file exception please?

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

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

发布评论

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

评论(1

动次打次papapa 2025-01-03 11:04:42

事实证明,我所需要做的就是将文件扩展名添加到重写条件中。另外,值得注意的是,重写条件必须出现在重写规则之前

RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|mp3|png|swf|xml)$
RewriteRule ^[^/]*/(gallery|admin|blog)(.+) $1$2 [QSA]
RewriteCond %{REQUEST_URI} !.*/(gallery|admin|blog)
RewriteRule !\.(css|js)$ index.php

Turns out all I needed to do was add the file extensions to the rewrite condition. Also it's VERY important to note that the rewrite condition must appear before the rewrite rule.

RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|mp3|png|swf|xml)$
RewriteRule ^[^/]*/(gallery|admin|blog)(.+) $1$2 [QSA]
RewriteCond %{REQUEST_URI} !.*/(gallery|admin|blog)
RewriteRule !\.(css|js)$ index.php
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文