Reputation:
THE INFO
I'm cleaning up a few URLS in my web app by doing some rewriting in NGINX.
The first rewrite I am doing is for handling paging [ex. http://domain.com/p/2], and the second is for a user profile area [ex. http://domain.com/username].
The rewrites I am using are as follow:
rewrite ^/p/(.*)$ /index.php?p=$1; # paging rewrite from /p/<page_number>
rewrite ^/(.*)$ /user.php?user=$1; # user page rewrite from /<username>
The problem
The problem I am having should be pretty easy to spot. Since I am using /p/ and /username, the rewrite doesn't differentiate between the two and ends up thinking /p/2 should be passed as the user page.
I know I need to run a check of /p/ and treat it differently than anything else to rewrite, but in my reading I have read that IF statements were to be avoided if possible due to potential unexpected results.
Question
Is this a case where an IF statement would be usable, if so what would that look like to ensure I am checking against the proper request coming from the user. If there is a way to do the check without an IF statement, I would like some insight into accomplishing that.
Appreciate any help.
Cheers,
Jared
Upvotes: 1
Views: 1510
Reputation: 18290
This is what the last flag is for. Just add it to the first rewrite, and if it matches, then the second rewrite won't be evaluated:
rewrite ^/p/(.*) /index.php?p=$1 last;
rewrite ^/(.*) /user.php?user=$1;
Upvotes: 3