Reputation: 23
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.
Upvotes: 2
Views: 351
Reputation: 49792
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.
Upvotes: 1