Categories: php, apache, nginx, url, url-rewriting

Rewrite URL to specifi query parts Nginx

1 answer

I have got a situation where I want to rewrite the following URLs and send my first URL parameter as data parameter and rest as query parameter      //Skip - Do Nothing because its a file from root          //anything == ?data=$1       //something == ?data=$1    b == &query=$2     //certainly == ?data=$1    b/c == &query=$2  //anything ==  ?data=$1    b/c.jpg == &query=$2         //but ==       ?data=$1    b/c/d == &query=$2 

so my first URL parameter will always be data and rest as query no matter what it has. I found this question here htaccess redirect only if it has value after forward slash and got it to some extent like

rewrite ^/(.*)/(.+)$ /handle.php?data=$1&query=$2 last; 

It has some problems as this rewrites into following      //Results in 404 Not Found          //Results in 404 Not Found       //Working good     //data=certainly/b    query=c  //data=certainly/b    query=c.jpg         //data=but/b/c        query=d 

I tried playing around with different variations in rewrite ^/(.*)/(.+)$ /handle.php?data=$1&query=$2 last; but nothing worked

All answers to this question, which has the identifier 61170567

The best answer:

So that I can prove the regex matching outcomes, I'll demonstrate with preg_replace(), but the pattern should seamlessly translate to your script.

Code: (Demo)

$urls = [     '',      //Skip - Do Nothing because its a file from root     '',          //anything == ?data=$1     '',       //something == ?data=$1    b == &query=$2     '',     //certainly == ?data=$1    b/c == &query=$2     '',  //anything ==  ?data=$1    b/c.jpg == &query=$2     '',         //but ==       ?data=$1    b/c/d == &query=$2 ];  var_export(     preg_replace('~www\.example\.com/(?![^/]+\.jpg)([^/]+)(?:/(.+))?~', '$1&query=$2', $urls) ); 


array (   0 => '',   1 => '',  // note the empty query value   2 => '',   3 => '',   4 => '',   5 => '', ) 

Using a negated character class containing the slash for the first capture group will permit greedy (speedy) matching without matching too much.

The negative lookahead prevents matching a jpg file.

The second capture group is optional, so unless you want to write a separate pattern to match data-only urls, you'll need to tolerate the empty assignment.

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type is referenced as an interface from”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data