Rewrite Regex Modification - Multiple Negative Images

I currently have a working relativistic redefinition:

RewriteEngine On RewriteCond %{QUERY_STRING} ^(.*)$ RewriteRule ^(.*/)?((?:cmd)[^/]*)/((?!(?:cmd)[.+]*)(.+)) $1?$2=$3&%1 [L] 

This regular expression takes the following URL and translates it into a URL immediately:

www.site.com/cmd1/param/cmd2/param2/stillparam2 and turn it into www.site.com/index.php?cmd1=param&cmd2=param2/stillparam2

This works well, but I would also like to create another negative statement to make sure the URL block, i.e. the /texthere/ does not contain an underscore. An invalid line might look like this: www.test.com/cmd/thing/getparam_valuehere ; the regular expression should parse cmd/thing as a pair of keys and values ​​and ignore the rest of the line. Then I will also write another RewriteRule so that the underlined URL block is added as another URL parameter. The following URL translation will appear:

 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 tried using a negative lookahead inside an existing one - (?!(?!)) - and tried to use | on two negative mappings, but none of them worked. I thought that maybe something else was fundamentally wrong?

Thanks to everyone.

Edit: I also tried the following - which I really thought worked (but obviously did not!)

 RewriteRule ^(.*/)?((?:cmd)[^/]*)/((?!(?:cmd)[.+]*)(?![.+]*(?:_)[.+]*)(.+)) $1?$2=$3&%1 [L] 

This does the following:

www.test.com/cmd/param1/sortorder_1/ means www.test.com?cmd=param1/sortorder_1/

When should he instead become: www.test.com?cmd=param1&sortorder=2/ . The rule for translating /sortorder_2/ to &sortorder=2 has not yet been created, but you can hope to see what I mean).

+6
source share
2 answers

After about four days of experiments, I got a slightly different solution than I expected from him. I simply deleted all the actual manipulations with the URL into the index.php file and sent all the requests 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 code block that 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]); 

At 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 creates these separate arrays, which I then use to process requests:

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

Thanks to everyone who took the time to read and respond.

+1
source

Wouldn't it be easier to save your working rule and rewrite param_value in the query string before your current rule?

Sort of

RewriteRule ^(.*)?/([^_/]+)_([^/]+)/ $1/?$2=$3 [N,QSA]

should add all / param _value / parts to querystring as param = value.

Be careful using the N flag, you may end up with an infinite loop.

0
source

Source: https://habr.com/ru/post/892958/


All Articles