How to urlencoder redirect url in Nginx? - nginx

When access this url: https://bar.com?type=search&keyword=foobar in PC, redirect to another url and whole source url as a request param,
https://baz.com/qrCode?from=https://bar.com?type=search&keyword=foobar
Nginx config:
if ( $http_user_agent !~* "(Android|iPhone)" ){
rewrite ^/(.*)$ https://baz.com/qrCode?from=https://bar.com$request_uri? permanent;
}
But exists this problem, that is the source url incomplete.
desired effect like below
How to implement is in Nginx?

Related

Rewrite URL's to Subdomain in NGINX

I have several url's that I would like to rewrite in NGINX
for example:
From: app.example.com/calendar
To: calendar.example.com
Or
From: app.example.com/meetings
To: meetings.example.com
I would still like to keep the app.example.com so it's not being removed from the redirect, but just create subdomains for certain URLs.
How can I do this in NGINX conf file?
All the best.
Redirect Subdomain to Folder in NGINX
Just add the following location block in your server configuration, inside server block, above the location / block.
location ^~ /calendar {
rewrite ^/calendar/?(.*)$ https://calendar.example.com/$1 permanent;
}
In the above code replace blog.example.com with your subdomain, and /calendar with your subdirectory. The above location block will listen to all requests sent to example.com/calendar. It redirects all those requests to calendar.example.com subdomain of your website. $1 is the URL stub after subfolder in requested URL. We add permanent keyword at the end of rewrite directive to cause a permanent redirect.
So it will permanently redirect subfolder to subdomain along with URL string.

Removes element in language url

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.

nginx substitude file sitemap.xml on subdomain

I have file sitemap.xml in site public directory. When I use subdomain 'm.', it uses same public directory as general site. I need to return file '/mobile-sitemap.xml' from url '/sitemap.xml', when I'm on subdomain 'm.'.
What nginx modules and rules could help me to do this?
My first step was like this:
if ($host ~* m\.(.*)) {
#if url = /sitemap.xml, give back file /mobile-sitemap.xml as /sitemap.xml
}
Or maybe it's ok to google, if i just send 301 redirect to another sitemap file?
You could create a separate server block for your mobile subdomain, in which case the if statement would not be necessary. See this caution on the use of if.
However, to implement this in a single server block, use a location and a rewrite:
location = /sitemap.xml {
if ($host ~* m\.) {
rewrite ^ /mobile-sitemap.xml last;
}
}
See this and this for more.

nginx redirect old URL to new URL

We just updated all our product url to new url. It only add some letter in the URL.
An example
old URL:
http://www.example.com/parent/children/product.html
new URL:
http://www.example.com/new-parent/children/product.html
(just added "new-" in "parent")
I try with this but not work.
location /parent {
rewrite ^/parent(.*) http://$server_name/new-parent$1 permanent;
}
So anyone can help me to correct this redirection?
Try to put rewrite ^/parent(.*) http://$server_name/new-parent$1 permanent; into server directive, not into /parent location.

Redirecting https site to http url in Nginx

I have this redirect script in my nginx.conf file. I want to redirect to http:// site but my origin is https:// how do I remove the 's' from the url? Adding the http:// to the pass won't work
rewrite ^(?!/mt/)(.*)$ /content$1 break;
Origin: https://blog.com/mt/content/apple/site.php
Redirect: http://blog.com/content/apple/site.php
Use protocol and host in redirect:
rewrite ^(?!/mt/)(.*)$ http://blog.com/content$1 break;

Resources