Mod 重写正则表达式 - 多个否定前瞻

发布于 2024-11-24 02:56:33 字数 1418 浏览 4 评论 0原文

我目前有可用的 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 to
www.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 技术交流群。

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

发布评论

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

评论(2

猫性小仙女 2024-12-01 02:56:33

经过大约四天的实验,我最终得到了一个与我最初预期有所不同的解决方案。我只是将所有实际的 URL 操作删除到我的 index.php 文件中,并将所有请求路由到那里。这是我的(更干净的).htaccess 文件:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{QUERY_STRING} (.*)
RewriteRule (.*) index.php?path=$1 [QSA,L]

这是我用来解析输入的 URL 的代码块:

preg_match_all('|/([A-Za-z0-9]+)((?! /)[A-Za-z0-9-.]*)|', $_GET['path'], $matches);

        // Remove all '$_GET' parameters from the actual $_GET superglobal:
        foreach($matches[0] as $k => $v) {
            $search = '/' . substr($v, 1);
            $_GET['path'] = str_replace($search, '', $_GET['path'], $count);
        }

        // Add $_GET params to URL args
        for ($i = 0; $i < count($matches[1]); $i++) {
            self::$get_arguments[$matches[1][$i]] = $matches[2][$i];
        }

        // Retrieve all 'cmd' properties from the URL and create an array with them:
        preg_match_all('~(cmd[0-9]*)/(.+?)(?=(?:cmd)|(?:\z))~', $_GET['path'], $matches);

        if (isset($matches[1][0])) {
            return self::$url_arguments = array_combine($matches[1], $matches[2]);

在这样的 URL 上:

http://localhost/frame_with_cms/frame/www/cmd/one/cmd2/two/cmd3/three/cmd4/four/getparam_valuepart1_valuepart2/cmd5/five/

它成功地生成了这些单独的数组,然后我用它们来处理请求:

Array
(
    [getparam] => valuepart1_valuepart2
)
Array
(
    [cmd] => one/
    [cmd2] => two/
    [cmd3] => three/
    [cmd4] => four/
    [cmd5] => five/
)

感谢所有花时间阅读和回复的人。

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:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{QUERY_STRING} (.*)
RewriteRule (.*) index.php?path=$1 [QSA,L]

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);

        // Remove all '$_GET' parameters from the actual $_GET superglobal:
        foreach($matches[0] as $k => $v) {
            $search = '/' . substr($v, 1);
            $_GET['path'] = str_replace($search, '', $_GET['path'], $count);
        }

        // Add $_GET params to URL args
        for ($i = 0; $i < count($matches[1]); $i++) {
            self::$get_arguments[$matches[1][$i]] = $matches[2][$i];
        }

        // Retrieve all 'cmd' properties from the URL and create an array with them:
        preg_match_all('~(cmd[0-9]*)/(.+?)(?=(?:cmd)|(?:\z))~', $_GET['path'], $matches);

        if (isset($matches[1][0])) {
            return self::$url_arguments = array_combine($matches[1], $matches[2]);

On a URL like this:

http://localhost/frame_with_cms/frame/www/cmd/one/cmd2/two/cmd3/three/cmd4/four/getparam_valuepart1_valuepart2/cmd5/five/

It successfully produces these separate arrays which I then use to handle requests:

Array
(
    [getparam] => valuepart1_valuepart2
)
Array
(
    [cmd] => one/
    [cmd2] => two/
    [cmd3] => three/
    [cmd4] => four/
    [cmd5] => five/
)

Thanks to all who took the time to read and reply.

雨后咖啡店 2024-12-01 02:56:33

保留您的工作规则并将 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.

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