一个正则表达式来匹配任何不以特定字符串开头的内容

发布于 2024-09-29 09:01:22 字数 575 浏览 1 评论 0原文

我正在编写一个 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 技术交流群。

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

发布评论

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

评论(2

錯遇了你 2024-10-06 09:01:24

我将创建一个单独的早期规则(正如Kobi也建议的那样)并使用RewriteRule dash 指令 以防止替换。

破折号表示没有替换
应该执行(现有路径
未受影响地通过)。这是
当标志(见下文)需要时使用
无需更改路径即可应用。

^index.php - [L]
^(admin|media) - [L]
^([A-Za-z0-9-_/\.]+)$ /?page=$1 [L]

(我现在无法测试这个,所以你可能需要稍微编辑一下 - 抱歉..但这个想法应该很清楚)

在 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.

A dash indicates that no substitution
should be performed (the existing path
is passed through untouched). This is
used when a flag (see below) needs to
be applied without changing the path.

^index.php - [L]
^(admin|media) - [L]
^([A-Za-z0-9-_/\.]+)$ /?page=$1 [L]

(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.

有木有妳兜一样 2024-10-06 09:01:24

如果您的版本支持它,您可以使用否定前瞻:

^(?!(?:admin|media)$)([A-Za-z0-9-_]+)/$

现在,我不是系统管理员,但您可能可以有一个较早的规则来匹配 adminmedia ,这可能是一个更简单的想法。

If your version supports it, you can you use a negative lookahead:

^(?!(?:admin|media)$)([A-Za-z0-9-_]+)/$

Now, I'm no sys admin, but you can probably have an earlier rule to match admin and media, which is probably an easier idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文