Nginx Bulk URL Redirect - nginx

I mapped URL in Nginx ( Bulk Level of Redirect)
nginx.conf
map $uri $new_url2{
include /var/www/html/**ext.map**;
}
=== **ext.map** Inside File
/en_us/abc.html /en_us/abc22.html;
/it_it/abc.html /it_it/abc22.html;
/fr_fr/abc.html /fr_fr/abc22.html;
nginx_host.conf
if ($new_url2)
{
return 301 $new_url2;
break;
}
It's working fine. Because of location block in URL, I need to generate multiple times for individual country wise.
I am trying to capture the location section and add with redirect URL.

You can use a regular expression in the map block by introducing it with a ~ or ~*. See this document for details.
Use a named capture to record the language code so that it can be used to construct the new URI. Use double quotes around an expression that contains braces {}.
For example:
"~*^(?<lang>/\w{2}_\w{2})/abc.html" $lang/abc22.html;

Related

How to block a specific URL in NGINX webserver

I want to block a specific URL but I am not able to do this.
The URL that should be blocked is example.com/clientarea/?dxx_g=dddd.
But the following url should still work - example.com/clientarea.
I tried the following:
location ^~ /clientarea/ {
return 444;
}
But if I do this it will block all connections to /clientarea.
I hope you can help me or advise me how to make this possible.
The location and rewrite statements test a normalized URI which does not include the ? and anything following it.
The $request_uri variable contains the entire URI. Test this variable using an if or map directive.
For example:
if ($request_uri = /clientarea/?dxx_g=dddd) {
return 444;
}
You can also use regular expressions. See this document for more. See this caution on the use of if.
If you have a number of URIs to block, you should consider using a map instead.

nginx case insensitive equals operator

I'm trying to manually redirect a couple of links from my old blog to my new blog like this:
location = /blog-article-url {
return 301 https://blog.example.com/blog-article-url
}
And this works when i visit https://www.example.com/blog-article-url, i get properly redirected. However it IS case sensitive, if i visit https://www.example.com/BLOG-ARTICLE-URL it will NOT work.
What should i replace the = sign in the nginx config block to make it case insensitive?
You can do a case insensitive location block with regular expressions.
For example:
location ~* ^/blog-article-url$ { ... }
Note that the evaluation order of regular expression locations is significant - so you may need to move this location block towards the top of your server block. See this document for more.

nginx rewrite map file, with or without a trailing slash

When using a map file to rewrite a large number of locations to their destinations:
rewrite ^ $my_redirect_map permanent;
Inside the map file, some redirects look like this (including trailing slash):
/foo/ /bar;
However, if nginx receives a request without a trailing slash, e.g. http://example.com/foo then the redirect doesn't occur.
It can be worked around by including duplicates of every entry in the map file (with and without the trailing slash.)
But is there some way to instruct nginx to ignore the trailing slash when processing the rewrite? It should work the other way too, ie. if the map file says /foo and the request says /foo/ it should match.
The problem is with the initial match in the map file. You cannot fix this problem at the rewrite statement.
The simplest solution would be to use a regular expression in the map directive's include file:
~^/foo/? /bar;
However, a less elegant solution would be to use two map directives, both including the same file of mappings:
map $uri $without {
include /path/to/file;
}
map $uri/ $with {
include /path/to/file;
}
server {
...
if ($with) { return 301 $with; }
if ($without) { return 301 $without; }
...
}
But the include file will need to specify a trailing / in order to match both cases.
See this document for details.

How do I restric access with nginx location to url matching an expression with a string and a number?

I have the following path as a Nginx Location :
/api/users
The url that my users access is /api/users?id=109238717283223
The problem is I don't want the direct path /api/users to be accessible, and the goal is to allow access only when the url contains users?id=number(with exact amount of digits)
How could I achieve this with a regex ?
Individual arguments can be accessed using the $arg_ variable prefix. It can be tested against a regular expression using an if directive. For example, an exact match location that ensures that the id parameter is exactly 15 digits:
location = /api/users {
if ($arg_id ~ ^\d{15}$) { return 403; }
...
}
Read this documentation regarding limitations using the if directive.

Ngine rewrite language directory

In my nginx.conf I want to rewrite the URL, except domains contain lang subfolder like this:
www.domain.com/vi/ or hwww.domain.com/en-us/
if user reaches
www.domain.com/whatever_not_lang/
then I will return another URL.
I got stuck at the condition "/vi/", I could return another URL, but it will return everything included /vi/ or /en-us/
Thanks....
In general if you want to 2 things for different url's on the same host, you create 2 locations, like so:
location /en-us/ {
# do one thing
}
location / {
# this catches everything else
# do your rewrite here
}
for multiple language codes you can either add multiple locations, or combine all of them as follows:
locaton ~* /(en-US|nl-BE|fr-FR)/ {
# handle the requests with a lang in them, lang being requested is available as
}

Resources