Rename args in nginx redirect - nginx

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.

Related

Rewrite one specific argument with nginx rewrite

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;

How can I match the following in Nginx

Dear StackOverflow Population,
I have been trying to get two simple things to work but I can't get but 1 of them to.
So I have nginx server block it looks like the following
server {
listen 443 default;
server_name secure.<domain>.com;
if ($host = $server_name) {
rewrite ^(.*) https://secure.<domain2>.com$request_uri?systpl=<template> permanent;
}
I have also tried since I know that IF is evil I have tried something like
return 301 https://secure.<domain2>.com$request_uri?systpl=<template>;
of course then I disable the if statement above, this works okay so when we goto
https://secure.domain.com => https://secure.domain2.com/?systpl=template
That works fine and when you enter a PHP ending such as
https://secure.domain.com/cart.php => https://secure.domain2.com/cart.php?systpl=template
This works both with IF and simple return, and I figure I need to get the other in a different server block or a statement but here is where the problem lies
Now if you goto
https://secure.domain.com/cart.php?id=7 => https://secure.domain2.com/cart.php?id=7?systpl=template
This of course doesn't work since it would have to be &systpl since it doesn't end with .php, how can I achieve this so it appends &systpl if it does not end with .php and ends with string and if not then append it with ?systpl if it ends with .php
I hope someone knows how this is done because I have crawled the first 6 pages of google results with various search strings.
If you are adding arguments, you should probably avoid using $request_uri and use $uri and $args instead.
The return statement could be rewritten as:
return 301 https://example.com$uri?systpl=template&$args;
The rewrite directive appends arguments by default, so this should work:
rewrite ^ https://example.com$uri?systpl=template permanent;
But as it is identical to the return statement, the return statement is preferred. See this document for details.

What's the correct nginx rewrite expression for this URL pattern?

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.

Case insensitivity in nginx

We have several SEO pages like:
http://www.example.com/PageOne.html
Which we redirect in config like:
location = /PageOne.html {
rewrite ^/(.*) /seo.php?id=1 last;
}
Problem is if a user access this page by typing:
http://www.example.com/pageone.html
"Page Not Found" error is displaying. There are approximate 500+ seo pages. How to write rule for nginx to ignore case sensitivity in url? I want a common solution for all url.
Specifically for PageOne.html, you can do the following:
location ~ /PageOne.html {
return 301 http://www.example.com/pageone.html$1;
}
If you have multiple URIs which need to be redirected, it appears the best option is to use Perl:
location ~ [A-Z] {
perl 'sub { my $r = shift; $r->internal_redirect(lc($r->uri)); }';
}
If you have hundreds of unique URIs which would involve many location blocks as above, I'd consider changing your application to handle lowercase URIs rather than expecting the webserver to handle the lowercase conversion.
This solved my issue. Sad to say that there is not many articles related to these issues, even nginx doesn't provide user friendly Help/Tutorials.
location ~* ^/-PageOne.html {
rewrite ^ /seo.php?page_id=1 last;
}
Hope this helps!

Nginx rewrite to css factory

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.

Resources