如何在RewriteCond中匹配子目录? [英] How to match subdirectories in RewriteCond?

查看:145
本文介绍了如何在RewriteCond中匹配子目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为RewriteCond使用正确的正则表达式遇到麻烦

Having trouble with proper regex for RewriteCond

RewriteCond %{REQUEST_URI} !^/foo/

按预期工作,也就是说,在重写之后,不适用于以/foo/开头的所有URL.

Works as expected, that is, does not apply following rewrite to all URLs that start with /foo/.

RewriteCond %{REQUEST_URI} !^/foo/bar/

另一方面,它不符合我的预期.以/foo/bar/开头的URL仍在被重写.

On the other hand does not work as I expect. URLs that begin with /foo/bar/ are still being rewrited.

如何输入适当的正则表达式以排除子目录?

How do I enter proper regex for excluding subdirectories?

推荐答案

也许这是规则应用到的内部重定向的新URL. L标志可以做到这一点.

Maybe it’s the new URL of an internal redirect the rule is applied to. The L flag does that.

[…]如果RewriteRule生成内部重定向(在每个目录上下文中重写时经常发生),这将重新注入请求并导致从第一个RewriteRule开始重复处理. /p>

[…] if the RewriteRule generates an internal redirect (which frequently occurs when rewriting in a per-directory context), this will reinject the request and will cause processing to be repeated starting from the first RewriteRule.

如果要确保初始URL路径不是以"/foo/bar"开头,请检查 THE_REQUEST变量)代替:

If you want to make sure that the initial URL path didn’t start with „/foo/bar", check the request line (see THE_REQUEST variable) instead:

RewriteCond %{THE_REQUEST} !^[A-Z]+\ /foo/bar/
RewriteRule …

这篇关于如何在RewriteCond中匹配子目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆