Nginx - redirect or rewrite url - nginx

We have nginx setup with location proxy pass and required one specific url need to redirect or rewrite when it was hit.
Src Url:
https://application-url:port/services/app1/callback/?oauth_token=<<tokens>>
Dest Url:
https://application-url:port/services/app1/callback?oauth_token=<<tokens>>
Any solution here.

It seems src and dest url are same. if you want to rewrite the url:
then add below lines in your server
location /callback {
rewrite ^/callback(.*) https://application-url:port/services/app1/callback$?oauth_token=<<tokens>> permanent
}

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.

Nginx: to show cleaner url on user side

I am trying to rewrite url so that on user side they would recive more clraner url.
Server url:
https://www.foo.com/news/article/pager.php?blog_id=2&tmpl_id=233?&page=2
On users browser url:
https://www.foo.com/news/article/page/2/
I have setup rewrite on nginx.conf like below but unsuccessful. Any idea why?
location / {
rewrite ^/article/page/([^/]+)/$ /article/pager.php?blog_id=2&tmpl_id=233&page=$1 break;
}

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;

NGINX Redirect or Alias?

I want to do a redirect in order to give a cleaner url for my users.
I want to change:
http://mydomain.com/main/username/profile
To:
http://mydomain.com/username/profile
Would I do this with a rewrite or an alian and how?
First you have to understand what the difference between a redirect and an alias is.
Redirect
A redirect will send a user who requests /main/username/profile to /username/profile. The URL will change in the browser. This is particularly important if the URL is accessible by search engines, because they would otherwise index the same page twice (duplicated content).
If you decide to use a redirect you should be sure, that your URLs stay that way. The reason for this is Cool URIs don't change.
Example for a redirect:
server {
# ...
location / {
# ...
location ~ /main/([a-zA-Z0-9]+)/profile$ {
# SEO effective redirect
return 301 /$1/profile;
}
# ...
}
}
nginx documentation: return
nginx wiki: return
Alias
An alias is used to tell nginx that that a requested file is not mapped by the URL on the filesystem and that it should have a look elsewhere. The following example is from the nginx wiki:
root /var/www;
location /i/ {
alias /spool/w3/images/;
}
A request for /i/empty.gif will not map to /var/www/i/empty.gif. Instead it will be matched to /spool/w3/images/empty.gif.
nginx documentation: alias
nginx wiki: alias

Resources