限制重写规则以排除某个目录

发布于 2024-09-19 02:05:31 字数 350 浏览 3 评论 0原文

当重写规则允许我们使用 ID 号创建友好的 URL 时。这个故事只是通过身份证号码拉出来的,所以最后的文字并不重要。

RewriteRule ^news/([0-9]+)$    /news/$1/ [R=301,L]
RewriteRule ^news/([a-zA-Z0-9_-]+)/.*$ /news/story.php?id=$1

当 /news/images/ 中链接的任何文件也会被重定向时,我们的问题就出现了。因此,任何显示来自 /news/images/ 的图像的内容都不起作用。

有人可以帮我吗?我们如何限制重写,使其显示“如果它位于 /images/ 子目录中,则不要重写路径”?

When a a rewrite rule to allow us to make friendly URL's with an ID number. The story is only pulled through the ID number, so the text at the end doesn't really matter.

RewriteRule ^news/([0-9]+)$    /news/$1/ [R=301,L]
RewriteRule ^news/([a-zA-Z0-9_-]+)/.*$ /news/story.php?id=$1

Our problem comes when any file linked within /news/images/ , it gets redirected as well. So anything that displays an image from /news/images/ doesn't work.

Can someone help me out? How can we limit the rewrite so that it says "If it's in the /images/ subdirectory, don't rewrite the path"?

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

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

发布评论

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

评论(1

十年不长 2024-09-26 02:05:31

您可以采取简单的路线,如果文件存在则避免重写:

RewriteRule ^news/([0-9]+)$    /news/$1/ [R=301,L]

# Ignore the RewriteRule if the request points to a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !=tags [NC]
RewriteRule ^news/([a-zA-Z0-9_-]+)/.*$ /news/story.php?id=$1

或者,如果您想进行目录检查,以防资源请求没有指向真实文件,但应该直接生成 404 响应,您可以尝试以下操作:

RewriteRule ^news/([0-9]+)$    /news/$1/ [R=301,L]

# Check if the path segment after /news/ corresponds to an existing directory
# and if so, don't perform the rewrite
RewriteRule %{DOCUMENT_ROOT}/news/$1/ !-d
RewriteCond $1 !=tags [NC]
RewriteRule ^news/([a-zA-Z0-9_-]+)/.*$ /news/story.php?id=$1

You could take the simple route and just avoid the rewrite if the file exists:

RewriteRule ^news/([0-9]+)$    /news/$1/ [R=301,L]

# Ignore the RewriteRule if the request points to a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !=tags [NC]
RewriteRule ^news/([a-zA-Z0-9_-]+)/.*$ /news/story.php?id=$1

Alternatively, if you wanted to do the directory checking in case the resource request didn't point to a real file, but should directly generate a 404 response, you can try the following:

RewriteRule ^news/([0-9]+)$    /news/$1/ [R=301,L]

# Check if the path segment after /news/ corresponds to an existing directory
# and if so, don't perform the rewrite
RewriteRule %{DOCUMENT_ROOT}/news/$1/ !-d
RewriteCond $1 !=tags [NC]
RewriteRule ^news/([a-zA-Z0-9_-]+)/.*$ /news/story.php?id=$1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文