How to rewrite url for url with multiple query parameter? - nginx

Server: Nginx
Application: PHP
URL in question is
/p/all_articles/user/ABC
/p/all_articles/user/ABC/page/123
which is
/index.php?p=all_articles&user=ABC
/index.php?p=all_articles&user=ABC&page=123
ABC = this could be a-zA-Z0-9 as it's username
123 = this could be page numbers 0-9
Tried many combinations and options, the following got me much closer to expected result, but it is not perfect.
location /p/all_articles {
rewrite ^/p/all_articles/user/(.*)?$ /index.php?p=all_articles&user=$1 last;
rewrite ^/p/all_articles/user/(.*)/page(?:/([0-9]+))?$ /index.php?p=all_articles&user=$1&page=$2 last;
try_files $uri $uri/ /index.php;
}
the above rewrite location block results only when one of them is quoted other one would work, but not together.
Similarly
/search/QUERY
/search/QUERY/page/123
which is
/index.php?search=QUERY
/index.php?search=QUERY&page=123
QUERY = anything a-zA-Z0-9 and space
123 = this could be page numbers 0-9
like the one for all_articles, the closest I was able to get is
location /search {
rewrite ^/search /index.php?search last;
rewrite ^/search/(.*)/page(?:/([0-9]+))?$ /index.php?search=$1&page=$2 last;
try_files $uri $uri/ /index.php;
}
But, this works to get either one of the clean URL when the other rewrite is quoted, but not together.
I appreciate if anyone give any answers to solve this issue.

The rewrite statements are evaluated in order so the more specific regular expression needs to be placed before a less specific regular expression.
With the two regular expressions:
^/search
^/search/(.*)/page(?:/([0-9]+))?$
The first one will match anything that also matches the second, so the second regular expression will never be evaluated.
The simple solution is to reverse the order of the rewrite statements in both of your location blocks.

Related

Nginx Configuration for Dynamic URL Segments as Arguments for Parent Segment Index

I am trying to set a location in the configuration that allows me to do something like https://example.com/car/<vin> which would not go to a <vin> application or directory but to /car/index.html. From there, I would read the URL or pass <vin> to /car/index.html.
I have tried various regex location blocks, like the one below, but they all result in a 404 when accessing /car/<vin>.
location ~ ^/car/(.*)$ {
root $document_root/car/
index index.html;
}
What would be an appropriate location block?
Do you want to use a regular expression? The prefix location would also work as it matches any URI that begins with /car/. See this document for details.
For example:
location ^~ /car/ {
try_files /car/index.html =404;
}
Using $document_root in the root statement may not work, and the index directive only works with URIs that end with a /. The try_files statement is probably the simplest solution. See this document for details.

convert url segment to get variable keeping others get variables

I can't fix that I have a url
domain.com/api/class/access/1/index.php?username=usuariodemo&name=sala
where /1/ is a ID so, I need convert /1/ to get variables like
domain.com/api/class/access/index.php?id=1&username=usuariodemo&name=sala
That is pure PHP not framework my index.php is located on folder api/class/access/index.php
I have this I try others but this explain me better waht I want do
location /api/class/access/(.*)/* {
try_files $uri $uri/ /api/class/access/index.php?id=$1&$query_string;
}
That show me:
No input file specified.
Thanks!
Regards!
The location statement in your question is invalid and probably unnecessary.
If you want to rewrite a URI ending with .php it may be easiest to place a rewrite statement inside the block that processes all .php URIs, usually: location ~ \.php$ { ... }.
For example:
location ~ \.php$ {
rewrite ^(/api/class/access/)(\d+)/(index.php)$ $1$3?id=$2 last;
...
}

Nginx location group capture + rewrite

I'm trying to do a group capture in a Nginx location block and it's not working for me.
Is what I am trying to do even possible?
location ~* /(?<cat>cars|trucks|bikes|motorcycle|quads) {
rewrite ^/$cat/([0-9]+)(.*)$ /page.php?id=$1 last;
}
The error message I am receiving is :
"^/$cat/([0-9]+)(.*)$" does not match "/cars/120/new-car-rentals/"
I have a lot more categories than what I am posting, and trying to prevent writing a rewrite 5x for each specific category name.
Any help would be appreciated.
I'm not familiar with this particular syntax, but based on my experience with others, is it possible that you simply need to escape the forward slashes you're using?
location ~* \/(?<cat>cars|trucks|bikes|motorcycle|quads) {
rewrite ^\/$cat\/([0-9]+)(.*)$ /page.php?id=$1 last;
}
Note the named capture in the location regex: if you want to use a value captured here, you must use the named syntax (?<name>), numbers do not work.
I solved the issue by doing this instead :
location ~* /(cars|trucks|bikes|motorcycle|quad-bikes) {
rewrite ^/([a-zA-Z-]+)/([0-9]+)(.*)$ /page.php?id=$2 last;
...
...
}
The regex ([a-zA-Z-]+) allows me to use characters a-z (case insensitive) with possible dashes in my category / page names.

Nginx rewrite anything after specific keyword

I want to rewrite all requests after "whois" keyword in url to whois.php in nginx but can't find suitable rules.
e.g. rewrite domain.com/whois.php/TEST.COM to whois.php?domain=TEST.COM.
There are a number of options available to you. One solution is:
location ~* ^/whois.php/ {
rewrite ^(/whois.php)/(.*)$ $1?domain=$2 last;
}
Place the location block above other regex locations that might match, as regex locations are executed on the basis of the first one that matches.
See this and this for more.

Having issues with regex matching wrong URLs in NGINX rewrite

Hi I have urls that look like this
http://dansawesomesite.com/123/articlename
I have the following rewrite rule in nginx
location ~* /(\d+)/([\+\w-\ ]+)/?$ {
try_files $uri /wpcontentredir.php?slug=$1;
}
This matches the above URL however the issue comes about when I have the following URL's
http://dansawesomesite.com/posts/630325/like
(as well as a number of similar)
These also end up getting matched which is correct based on my regex, but will mess things up as I dont want these urls parsed through that try_files, I just want them to pass as is.
Just wondering if anyone can help me with only matching the top first URL?
CHeers
Dan
Try to add "^" to the beginning of regexp
location ~* ^/(\d+)/([+\w-\ ]+)/?$
So it will match only if first part of URI contains digits and not "posts" or something

Resources