Mod 重写正则表达式 - 多个否定前瞻
我目前有可用的 Mod Rewrite Regex:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*/)?((?:cmd)[^/]*)/((?!(?:cmd)[.+]*)(.+)) $1?$2=$3&%1 [L]
该正则表达式采用以下 URL 并将其转换为紧接下面的 URL:
www.site.com/cmd1/param/cmd2/param2/stillparam2
并将其转换为www.site.com/index.php?cmd1=param&cmd2=param2/stillparam2
效果很好,但我还想创建另一个否定的先行断言以确保 URL 块 -即 /texthere/
参数 - 不包含下划线。无效字符串可能如下所示:www.test.com/cmd/thing/getparam_valuehere
;正则表达式应该将 cmd/thing
解析为键和值对,并忽略字符串的其余部分。然后,我还会编写另一个 RewriteRule,将带有下划线的 URL 块添加为另一个 URL 参数。将会发生以下 URL 翻译:
www.test.com/cmd/param1/cmd2/directory/param2/sortorder_5
www.test.com?cmd=param1&cmd2=directory/param2&sortorder=5
如果我说得不够清楚,请告诉我。任何帮助都会很棒。
注意:我尝试使用嵌套在已存在的负前瞻中 - (?!(?!))
- 并尝试在两个负前瞻上使用 |
,但这两种解决方案都不起作用。我想也许还有其他更根本的错误?
谢谢大家。
编辑:我还尝试了以下操作 - 我真的认为它会起作用(但显然没有!)
RewriteRule ^(.*/)?((?:cmd)[^/]*)/((?!(?:cmd)[.+]*)(?![.+]*(?:_)[.+]*)(.+)) $1?$2=$3&%1 [L]
它执行以下操作:
www.test.com/cmd/param1/sortorder_1 /
翻译为 www.test.com?cmd=param1/sortorder_1/
当它应该变成:www.test.com?cmd=param1&sortorder=2/
。将 /sortorder_2/
转换为 &sortorder=2
的规则尚未创建,但您希望能明白我的意思)。
I currently have the working Mod Rewrite Regex:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*/)?((?:cmd)[^/]*)/((?!(?:cmd)[.+]*)(.+)) $1?$2=$3&%1 [L]
That regex takes the following URL and transforms it into the URL immediately below:
www.site.com/cmd1/param/cmd2/param2/stillparam2
and turn it into www.site.com/index.php?cmd1=param&cmd2=param2/stillparam2
That works fine, but I would also like to create another negative lookahead assertion to ensure that a URL block - ie a /texthere/
param - doesn't include an underscore. An invalid string might look like: www.test.com/cmd/thing/getparam_valuehere
; the regex should parse the cmd/thing
as a key and value pair and ignore the rest of the string. I would then also write another RewriteRule to have the block of the URL with the underscore in it added as another URL parameter. The following URL translation would occur:
www.test.com/cmd/param1/cmd2/directory/param2/sortorder_5
www.test.com?cmd=param1&cmd2=directory/param2&sortorder=5
Please let me know if I have not been clear enough. Any help would be great.
NB: I have tried using a negative lookahead nested inside the one already present - (?!(?!))
- and have tried using an |
on two negative lookaheads, but neither solutions worked. I thought that perhaps something else was more fundamentally wrong?
Thanks all.
Edit: I have also tried the following - which I really thought would work (but obviously, didn't!)
RewriteRule ^(.*/)?((?:cmd)[^/]*)/((?!(?:cmd)[.+]*)(?![.+]*(?:_)[.+]*)(.+)) $1?$2=$3&%1 [L]
That does the following:
www.test.com/cmd/param1/sortorder_1/
translates towww.test.com?cmd=param1/sortorder_1/
When it should instead become: www.test.com?cmd=param1&sortorder=2/
. The rule to translate /sortorder_2/
into&sortorder=2
has not yet been created, but you can hopefully see what I mean).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过大约四天的实验,我最终得到了一个与我最初预期有所不同的解决方案。我只是将所有实际的 URL 操作删除到我的 index.php 文件中,并将所有请求路由到那里。这是我的(更干净的).htaccess 文件:
这是我用来解析输入的 URL 的代码块:
preg_match_all('|/([A-Za-z0-9]+)((?! /)[A-Za-z0-9-.]*)|', $_GET['path'], $matches);
在这样的 URL 上:
它成功地生成了这些单独的数组,然后我用它们来处理请求:
感谢所有花时间阅读和回复的人。
After about four days of experimenting, I ended up with a somewhat different solution than I had originally expected to find. I simply removed all the actual URL manipulation to my index.php file and routed all requests through there. Here is my (much cleaner) .htaccess file:
and here is the block of code I used to parse the entered URL:
preg_match_all('|/([A-Za-z0-9]+)((?!/)[A-Za-z0-9-.]*)|', $_GET['path'], $matches);
On a URL like this:
It successfully produces these separate arrays which I then use to handle requests:
Thanks to all who took the time to read and reply.
保留您的工作规则并将 param_value 重写为当前规则之前的查询字符串不是更容易吗?
像
RewriteRule ^(.*)?/([^_/]+)_([^/]+)/ $1/?$2=$3 [N,QSA]
之类的内容应该附加所有 /param_value / 作为 param=value 插入查询字符串中。
使用 N 标志时要小心,您可能会陷入无限循环。
Wouldn't it be easier to keep your working rule and rewrite the param_value to the query string before your current rule?
Something like
RewriteRule ^(.*)?/([^_/]+)_([^/]+)/ $1/?$2=$3 [N,QSA]
should append all /param_value/ parts in into the querystring as param=value.
Take care using the N flag, you might end up with an infinite loop.