Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess 限制重写规则以排除特定目录_.htaccess_Mod Rewrite_Url - Fatal编程技术网

.htaccess 限制重写规则以排除特定目录

.htaccess 限制重写规则以排除特定目录,.htaccess,mod-rewrite,url,.htaccess,Mod Rewrite,Url,当一个用户使用重写规则,允许我们使用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/的图像的任何内容都不起作用 有人能

当一个用户使用重写规则,允许我们使用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/子目录中,请不要重写路径”

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

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

实际上,基本上我们希望重写规则“如果它在/news/下面的任何目录中,请不要重写url。非常感谢您的帮助,Tim。您的第一个解决方案几乎完全有效。我们遇到的最后一个问题是,我们还需要对标记进行重写,它会转到/news/tags/。所以我们需要确保如果URL有/news/tags/也不要重写为story.php。@scatteredbomb-没问题。为了完整性,我继续编辑了答案,并对该场景进行了额外检查,尽管对于第一个选项,在故事重写之前使用
L
标志进行标记重写也是一种可能的解决方案。再次感谢。我希望我能给你更多的分数:)我真的很感谢你的帮助。