一个正则表达式来匹配任何不以特定字符串开头的内容
我正在编写一个 mod_rewrite 规则,希望有人能帮我实现我想要的功能。
基本上我希望以下情况能够工作:(
request | Redirect to
---------------------
cheese | /?page=cheese
abc123 | /?page=abc123
abc/def | /?page=abc/def
a/b/c/d | /?page=a/b/c/d
任何字母数字加 . - / 和 _ 重定向)
有两种特殊情况:
request | Redirect to
------------------------
admin | don't redirect
media | don't redirect
对于这两种情况,我不希望任何子目录等重定向。
我已经得到了:
RewriteRule ^([A-Za-z0-9-_]+)/$ /index.php?page=$1 [L]
但这只满足了前两个测试。
有什么想法吗?
I'm writing a mod_rewrite rule and would like a hand making it do what I want.
Basically I want the following cases to work:
request | Redirect to
---------------------
cheese | /?page=cheese
abc123 | /?page=abc123
abc/def | /?page=abc/def
a/b/c/d | /?page=a/b/c/d
(Any alphanumeric plus . - / and _ redirected)
With two special cases:
request | Redirect to
------------------------
admin | don't redirect
media | don't redirect
With these two I don't want any sub directories etc to redirect either.
I've got as far as:
RewriteRule ^([A-Za-z0-9-_]+)/$ /index.php?page=$1 [L]
But this only satisfies the top two tests.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将创建一个单独的早期规则(正如Kobi也建议的那样)并使用
RewriteRule dash 指令
以防止替换。(我现在无法测试这个,所以你可能需要稍微编辑一下 - 抱歉..但这个想法应该很清楚)在 Apache 2.2 上测试
编辑:是现在就可以尝试这个 - 需要添加 [L] 才能使其工作。
编辑2:意识到我没有回答完整的问题,为其余的内容添加了规则。
I would create a separate earlier rule (as Kobi also suggested) and use the
RewriteRule dash directive
to prevent substitution.(I wasn't able to test this where I'm at right now, so you might need to edit this slightly - sorry .. but the idea should be clear)Tested on Apache 2.2
EDIT: Was able to try this out now - needed to add the [L] to make it work.
EDIT2: Realized I didn't answer the full question, added rules for the rest of the stuff.
如果您的版本支持它,您可以使用否定前瞻:
现在,我不是系统管理员,但您可能可以有一个较早的规则来匹配
admin
和media
,这可能是一个更简单的想法。If your version supports it, you can you use a negative lookahead:
Now, I'm no sys admin, but you can probably have an earlier rule to match
admin
andmedia
, which is probably an easier idea.