I want to rewrite an argument given via URL on my nginx server from "my_arg" to "my-arg". The problem is that I can't know how many arguments there will be in advance nor the position of these arguments.
I can only know if the arguments is there using :
if ($args ~ (.*)my_arg=(.*)) {
rewrite ....
}
But i cannot achieve to only change the name of this argument without changing every others.
Thanks by advance
For anything except HTTP 301/302 redirection you'd better just change an $args variable value:
if ($args ~ (^|.*&)my_arg=(.*)) {
set $args $1my-arg=$2;
}
If you want to generate a redirect, things will be more complicated. Since any rewrite directive performs its own URI matching against some regex pattern, all the $1, $2 and so on variables from previous matching will be overwritten, and you can't match a query part of the URI to get them re-evaluated since rewrite directive works with normalized URI that doesn't include a query part. Your only way is to use named capture groups:
if ($args ~ (?<prefix>^|.*&)my_arg=(?<suffix>.*)) {
rewrite ^ $uri${prefix}my-arg=$suffix? <redirect|permanent>;
}
Trailing question mark used intentionally since as rewrite directive documentation says,
If a replacement string includes the new request arguments, the previous request arguments are appended after them. If this is undesired, putting a question mark at the end of a replacement string avoids having them appended, for example:
rewrite ^/users/(.*)$ /show?user=$1? last;
Related
I am trying to get an NGINX redirect to take me from:
https://example.com/ui/?outletID=123&setID=456
to:
https://example.com/ui/?outletId=123&setIds=456
So outletID -> outletId AND setID -> setIds
IMPORTANT: I don't know where these params appear inside the URL, so there might be other strings before or after these. I only care for this replacements: outletID -> outletId; setID -> setIds.
This works at first try:
if ($args ~* "^outletID=(\d+)&setID=(\d+)(.*)") {
set $outletid $1;
set $setid $2;
set $everythingelse $3;
set $args '';
rewrite ^.*$ /ui/?outletId=$outletid&setIds=$setid$everythingelse permanent;
}
But it looks like really bad practice and I particularly hate the $everythingelse ($3) solution I ended up with.
To cover all possibilities, you probably need to do this in two stages, one redirect if outletID is present, and another redirect if setID is present.
For example:
if ($args ~ ^(.+&|)outletID(=.*)$) {
return 301 "$uri?${1}outletId$2";
}
if ($args ~ ^(.+&|)setID(=.*)$) {
return 301 "$uri?${1}setIds$2";
}
Per Nginx documentation,
If a replacement string includes the new request arguments, the
previous request arguments are appended after them.
So, in case you don't mind having old outletID and setID hanging around, you could simply do:
rewrite (.*) $1?outletId=$arg_outletID&setIds=$arg_setID permanent;
This way, you'll get something like https://example.com/ui/?outletId=123&setIds=456&outletID=123&setID=456.
But if you would like to have the URLs clean after redirection, Nginx has you covered as well. The excerpt from the same documentation:
If this is undesired, putting a question mark at the end of a replacement string avoids having them appended
Which means, you have to enumerate all the parameters you need to pass. For example, if you have some another possible parameter in query string session, you'll need to include it:
rewrite (.*) $1?outletId=$arg_outletID&setIds=$arg_setID&session=$arg_session? permanent;
Note the ? in the end of replacement string.
Personally I would choose the first approach, because it is less error-prone and will not break your application unexpectedly at the time you added a new parameter.
I'm going bananas trying to get a Nginx rewrite rule to work. An example URL that the rewrite pattern should match is:
https://test.mydomaind.com/abc.php?id=1
My rewrite rule:
rewrite ^/abc\.php?id=(.*)$ https://test.mydomain.com/page/$1 permanent;
But this returns a 404 with the example URL.
Can anyone tell me what I'm doing wrong? When I leave out the query string parameter in the condition the rewrite does work and the user is redirected 301:
rewrite ^/abc\.php$ https://test.mydomain.com/page permanent; # THIS WORKS
Any help is greatly appreciated!
Anything from the ? onwards is the query string and is not considered when matching the URI to locations or rewrite statements.
The query string (or arguments) are accessible in the $request_uri and $query_string (aka $args) variables and the family of variables prefixed with $arg_.
You could implement something similar with:
location = /abc.php {
return 301 https://test.mydomain.com/page/$arg_id;
}
If you need to test for the presence of an id= argument, you will need to resort to map or if statements.
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.
I have a rule which is not working correctly.
I need it so that whenever URL xxx.com/forum/css.php is hit, it is re-written to xxx.com/forum/core/css.php.
I have written the following location block for it:
location ~^ /forum/css.php {
rewrite ^ /forum/core/css.php permanent;
}
Also needing to be taken into account is that the file is a factory so it accepts parameters, the url being hit actually looks like xxx.com/forum/css.php?x=123&y=string. Will this also be taken into account in the re-writes or does it need to be specified? Sorry if the question seems silly I am just beginning to work with servers! Thanks fellow coders!
To rewrite a single URI (with or without query string) you could use a location =:
location = /forum/css.php {
rewrite ^ /forum/core/css.php permanent;
}
The rewrite directive appends the query string (unless terminated with ?). See this and this for more.
I have a site with two RESTful URL schemes:
foo.com/model/id and
foo.com/model/id/action
The actual pages served by these URLs are in the form of
$model.php?id=$id and
$model_$action.php?id=$id respectively.
I have a single regular expression that will match both cases ^(\w+)s/([A-z0-9]+)(?:/(\w+))?/?$ and I'd like to use a single Nginx rule to rewrite for both types of URLs, but I'm not sure how to do this. In pseudocode I imagine the rule would look something like this
rewrite ^(\w+)s/([A-z0-9]+)(?:/(\w+))?/?$ /(($3) ? $1_$3.php?$id=$2 : $1.php?$id=2)
This isn't valid syntax (so far as I know), but can something like this be done in Nginx?
Rewrite the possible urls in turn starting with the longest to the shortest to that overlapping matching strings, "/model/id" in this case, would be matched in the longer url string first.
location / {
# For "/model/id/action" to "$model_$action.php?id=$id"
rewrite ^/(.+)/(.+)/(.+)(/?)$ $1_$3.php?id=$2 last;
# For "/model/id" to "$model.php?id=$id"
rewrite ^/(.+)/(.+)(/?)$ $1.php?id=$2 last;
}
location ~ .+\.php$ {
# Handle PHP
}
The "(/?)" is just in case the urls sometimes come with an ending slash. If they never do, it can be removed. In this case, it will probably be best to specifically always add a closing slash and have "(/?)" as "/".