Removes element in language url - nginx

Currently I have a multilingual PHP website and have the following URL structure: https://qrcodeaz.com/?lang=vi.
Now I want to remove the element ?lang= from the URL and only leave: https://qrcodeaz.com/vi.
I use Nginx, please help me with the code.
Thank you

You can use the following code (before any of the location blocks):
if ($args ~ (.*)(^|&)lang=[^&]*(\2|$)&?(.*)) {
set $lang $arg_lang;
set $args $1$3$4;
}
if ($lang) {
rewrite (.*) /$lang$1;
}
Please note that this code would not generate HTTP redirect, https://qrcodeaz.com/?lang=vi URL will be served as https://qrcodeaz.com/vi transparently to end user. If you want to generate HTTP 301/302 redirects, use rewrite (.*) /$lang$1 permanent; for HTTP 301 redirection or rewrite (.*) /$lang$1 redirect; for HTTP 302 redirection.

Related

Rewrite Nginx Url only on specific path

So I have a main path https://app.example.com and I want it to keep it that, way but on all https://app.expample.com/p/* paths I want it to be rewritten to a short version of the url like. How can I achieve this?
I tried
location = /p/(.*)$ {
rewrite ^ https://example.com/p/$request_uri permanent; and also return 301 https://example.com/p/$request_uri;
}
but this resulted in too many http redirects and an error

Nginx Redirects

I'm struggling with a nginx redirect that I've set up.
Basically, I want to redirect /case-study/ to /case-studies/ but don't want to redirect any url past /case-study/*.
I have this setup already:
location = /case-study/ {
rewrite ^(.*)$ /case-studies/ permanent;
}
Which works for /case-study/ but also redirects any url after /case-study/test-url-redirect
Cany anyone help me out on this one.
The simplest way is to make it a regex location like so:
location ~ /case-study(/?)$ {
return 301 /case-studies/;
}
Should match /case-study and /case-study/ and redirect to /case-studies/ but not redirect anything that is under case-study

Redirect AMP files on nginx

I'm trying to make redirect on nginx but unfortunately it doesn't work.
What I'd like to achieve, it is to redirect amp files on mobile.
What I'd like to do :
from
https://www.example.com/uri-759.html
to
https://www.example.com/uri-759-amp.html
What I did as redirect
if ($mobile_redirect = perform) {
redirect ^(.*)(\.html)$ $1-amp$2 permanent;
}
what I obtain
https://www.example.com/uri-759-amp-amp-amp-amp-amp-amp-amp-amp.html
Does someone have a solution to perform this redirection ?
You can use a negative lookbehind assertion to avoid matching the rewritten URI.
For example:
rewrite ^(.*)(?<!-amp)(\.html)$ $1-amp$2 permanent;
See this document for more.

nginx rewrite rule to prepend something to a url

I want to rewrite all my JPG file URLs using mobify CDN. For that, all I have to do is prepend the URL
https://ir0.mobify.com/jpg50/ to my existing URL. So for example, if I have the URL
http://xxx.yyy.com/wp-content/uploads/2290/07/abc.png then the user has to be redirected to
https://ir0.mobify.com/jpg50/http://xxx.yyy.com/wp-content/uploads/2290/07/abc.jpg
I wrote the following code in my nginx config. I tested the regexs at regexlib and they seem to be fine.Still do not understand what is wrong with my config. Please help.
location ~ \.jpg$
{
rewrite ^http://(.*).jpg$ https://ir0.mobify.com/jpg50/$uri last;
}
Try this ...
location ~ \.jpg$ {
return 301 https://ir0.mobify.com/jpg50$request_uri;
}

Redirect a specific URL to another in NGINX

I need to redirect a specific url containing a parameter and an anchor to a new URL:
/pages/page.php3?page=fond_razeni_gk.htm#GKI
to
http://www.newserver.com/o-knihovne
I try:
rewrite ^/pages/page.php3\?page=fond_razeni_gk.htm$ http://www.newserver.com/o-knihovne? redirect;
but this does not work. Could you please advise me what is wrong?
try this configuration:
location / {
rewrite ^/pages/page.php3(.*)$ http://www.newserver.com/o-knihovne$1 permanent;
}

Resources