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;
Related
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.
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.
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?
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;
I conisder moving to Nginx but I want you to ask if is possible to rewrite urls into that schema, and if you could help me a bit:
A url like http://example.com/username into profile.php?u=username. But then, will I have problems in accessing other pages like e.g. home.php
A url like http://example.php/questions/102039/that-question into questions.php?quid=102039
Thank you very much!
Yes, it is possible to rewrite URLs with Nginx.
Your first example can be handled easily by wrapping the rewrite with a block which checks if a file (home.php in your example) exists; if it doesn't, then it tries the redirect into the profile.php request. (See the try_files syntax for this.)
Your second example is just as simple:
Rewrite ^/questions/(\d+) /questions.php?quid=$1
(Because the matching expression is not anchored at the end, it should accept any string after the digits, but I haven't checked that so I'd recommend you test it.)