Ngine rewrite language directory - nginx

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
}

Related

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 Bulk URL Redirect

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;

Nginx rewrite individual urls with get parameters to pretty urls

I have categories I'd like to rewrite.
for example:
example.com/videos?c=18
to:
example.com/category/name
I tried to do this using multiple examples, most had no effect and this example gave me only page not found to all /videos pages:
location /videos {
if ($args ~ "c=18") {
rewrite ^/videos(.*) http://$server_name/category/name$1 permanent;
}
}
Is this even doable purely via Nginx what I am trying to achieve?
As far as I got the question, currently your site has this url scheme, which you can't change:
http://example.com/videos?c=18
But you would like to present visitors with "pretty"-looking URLs like
http://example.com/category/name
That pretty URL does not really exist anywhere on the site, which is why you have to rewrite it, e.g. turn pretty virtual url into a real one that your scripts can process.
Once again, you rewrite from virtual to actual, not the other way round.
The following directive would turn /category/cars/ into /videos?c=cars
location /category {
rewrite ^/category/(.*)$ /videos?c=$1 last;
}
But your script won't understand /videos?c=cars url, it needs category ID to work. So in your case the pretty url should look like
http://example.com/category/18
which will be rewritten to
http://example.com/videos?c=18
"if" directive is not best solution, but in your case you can try "if" and $arg_name (argument name in the request line):
location /videos {
if ($arg_c = "18") {
rewrite ^/videos(.*) http://$server_name/category/cars? permanent;
}
if ($arg_c = "19") {
rewrite ^/videos(.*) http://$server_name/category/bikes? permanent;
}
# and so on
}

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 Issue to External link

I'm trying to set up a permanent redirect from
http://domain.com/member/blog_post_view.php?postId=1
to
http://blog.domain.com/friendly-url-here
The source URL contains both a ? and an = which I think might be the cause but am unsure.
I've tried all sorts of nginx suggestiosn including the one below but can't seem to get the redirection to work and hoped someone can point me in the right direction.
location /blog_post_view.php?postId=1 {
rewrite "/blog_post_view.php\?postId\=1" http://blog.domain.com/friendly-url-here permanent;
}
That part of request line starting from the question mark is called query string, while the location directive matches only path part of URI.
You should use $arg_* variable instead:
location =/blog_post_view.php {
if ($arg_postId = 1) {
return 301 http://blog.domain.com/friendly-url-here;
}
}
Reference:
http://nginx.org/r/location
http://nginx.org/r/if
Embedded Variables

Resources