rewrite URL with query parameters in path - nginx

So I need to rewrite:
mysite.com/page?type=2
to:
mysite.com/page/type/2
but not affect url's like:
mysite.com/page/type/2
I have tried using:
rewrite ^/page?.*$ /page/type/$arg_type? permanent;
but for some reason this is catching and rewriting the last case (where I don't want the url rewritten) in a loop. I wouldn't think it would get caught in a redirect loop due to the question mark exempting it, but maybe I don't fully understand the syntax...
Thanks for any help. It is greatly appreciated.

Try this:
rewrite ^/page/?$ /page/type/$arg_type? permanent;

Related

nginx rewrite: rewrite and pass original URI arguments along

I simply want to rewrite:
https://example.com/id/123?a=1&b=2 (and maybe more variables, or maybe none)
to
https://example.com/load.php?id=123&a=1&b=2 (...and whatever others, or maybe none after id)
That's it. So simple, right?
Yet I think there is a secret unwritten rule that says docs for nginx URL rewriting must be dense, unclear, and devoid of any useful examples. ;-) I have googled and experimented for a while now. Can't get it to fly.
I tried this, and several variations of it, with no luck:
rewrite ^/id/(.*)$ /load.php?id=$1&$args last;
I think I'm close. Can you help?
The rewrite directive automatically appends the original arguments (adding a ? or & separator as required) unless the replacement string ends with a ?.
For example:
rewrite ^/id/(.*)$ /load.php?id=$1 last;
The above will internally redirect /id/123?x=abc to /load.php?id=123&x=abc.
See this document for details.

Rewrite rule with nginx replace get variable with slach

I've been reading everything i could find for two days now but nothing seems to work
I have an url like this
http://www.mysite.com/auction.php?category=23140
and want make a rewrite rule so it will appears like that
http://www.mysite.com/auction/category/23140.php
I do that only in order to build seo friendly url but so far nothing have worked.
rewrite ^/auction.php?category=(.*)$ /auction/category/$1 last;
It look so simple i realy don't know why it doesn't work.
Should i put this line in the server block or the location block ? Is the regex wrong ?
Any help will be greatly appreciated
At first, pls remember rewrite in nginx does not match query string, only uri.
For example, in this url: "/auction.php?category=23140", uri is /auction.php and query string is category=23140
So in your case, the correct rule should be:
if ( $request_uri ~ "^/auction\.php\?category=[0-9]+(.*)?$" ) {
rewrite "^.*$" /auction/category/$arg_category break;
}
Above is just a example, you can change it according to your case. And also you can find the usage of $request_uri and $arg_PARAMETER from the nginx manual.
You're doing it backwards, try this.
rewrite ^/auction/([^/]*)/([0-9]*) /auction.php?$1=$2
piece of advice, if this is a custom application you're writing without a framework, you could find a routing module that you could use and it would help you do every thing easily.

How do I escape question mark with nginx rewrite?

Im bussy migrating my websites to nginx and I have a lot of rewrite rules that need to be converted, the only problem I'm experiencing is when I try to do something like this:
rewrite ^/media?(.*)$ /in.php?id=$1 last;
This causes php to read $1 ad the key of $_GET instead of it's value. Is there any way to change this behaviour without resorting to difficult location based methods? The reason I'm asking is because I have multiple query string based rewrites that need to be addressed.
Everything what comes after ? in nginx is known as variable $query_string, so if you want to translate whole query string from location rule ^/media?(.*)$, because it looks like it's what you want, try to use this:
rewrite ^/media?(.*)$ /in.php?id=$query_string last;
Is it what are you looking for?

Basic NGINX url rewriting

I have a domain with some get arguments.
Lets say 3, so we have this: www.domain.com/?a1=x&a2=y&a3=z
And I want to rewrite this to this: www.domain.com/x/y/z/
This is what I have till now
rewrite ^/(.*)\/(.*)\/(.*)/$ /index.php?a1=$1&a2=$2&a3=$3? last;
And this pretty much does the job when all arguments are filled in.
But! When you leave 1 argument out you get a 404.
So any NGINX expert who can help me rewriting the url?
I also need to exclude .js & .css files and can't do this for some reason as well.
Kind regards,
WouterDS
If you're xyz variables are alpha numeric you could just do something like:
rewrite ^/([a-zA-Z0-9]*)/?([a-zA-Z0-9]*)/?([a-zA-Z0-9]*)/?$ /index.php?a1=$1&a2=$2&a3=$3? last;

NGINX rewrite without IF statement

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
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;

Resources