gpt4 book ai didi

regex - 匹配任何不以特定字符串开头的正则表达式

转载 作者:行者123 更新时间:2023-12-04 06:42:56 24 4
gpt4 key购买 nike

我正在编写一个 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]

但这仅满足前两个测试。

有任何想法吗?

最佳答案

我会创建一个单独的 更早的 规则(正如 Kobi 也建议的那样)并使用 RewriteRule dash directive 以防止替代。

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]

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

在 Apache 2.2 上测试

编辑:现在可以尝试了 - 需要添加 [L] 以使其工作。
EDIT2:意识到我没有回答完整的问题,为其余内容添加了规则。

关于regex - 匹配任何不以特定字符串开头的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4027454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com