What is the syntax for a nginx url rewrite - nginx

I have an app running at
http://localhost/subfolder/myapp/
How do I create a rewrite rule so that the following urls get redirected to my app?
http://localhost/subfolder/myapp/route1
http://localhost/subfolder/myapp/route2
http://localhost/subfolder/myapp/routeN
...anyother routes after http://localhost/subfolder/myapp/

add this block nginx rules
location ~ /subfolder/myapp/.* {
redirect from here
}

In Nginx you can do rewrites with a return inside your server block:
location ~ /subfolder/myapp/route(.*) {
return 301 http://localhost/subfolder/myapp;
}

This seems to work:
rewrite ^/subfolder/myapp/.+$ /subfolder/myapp last;

Related

Nginx rule to redirect to a blog subdomain just spins

I have tried some Nginx rewrite for my new site
Blog
www.qwerty.com/blog/category/educator-blog/
blog.qwerty.com/educator/
DesiredURL, Actual URL, Nginx_Rule
server {
server_name www.example.com/blog/category/educator-blog/;
return 301 $scheme:// blog.example.com/educator/$request_uri;
}
Also tried this :
location /educator {
rewrite ^/educator(.*)$ http://www.qwerty.com/blog/category/educator-blog/$1 redirect;
rewrite ^/educator-categoryid-(.*)-productid-(.*).htm$ /educator.php?categoryid=$1&productid=$2;
}
I've rebuilt with the applied rules but it seems to just spin, some fixed URL. Does the order of the rules matter?

Permanent redirection between folders with nginx?

On the nginx configuration file
default
at
/etc/nginx/sites-enabled/
I am trying to redirect requests made to the folder "home" to another folder "jp".
Following the nginx manual,
I tried the script below. Any ideas as to why this would't work? Thanks.
server{
...
server_name _localhost;
location /home/ {
rewrite www.example.io/home/$ www.example.io/home/jp/ permanent;
}
}
You want a permanent redirection from /home/ to /home/jp/.
The first parameter to a rewrite directive is a regular expression which is matched against a normalized URI, in your case /home/.
You can use a rewrite directive, for example:
location /home/ {
rewrite ^/home/$ /home/jp/ permanent;
...
}
Alternatively, you could use an exact match location with a return statement, for example:
location = /home/ {
return 301 /home/jp/;
}

Nginx rewrite URL, change parts of URL

Is it possible in Nginx to rewrite URL like this?
http://www.example.com/city/person/contact.php?id=name
to
http://www.example.com/city/person/name/contact
How about:
location ~ /city/person/(.*)/(.*) {
rewrite ^/city/person/$2.php?id=$1 last;
}

Rewrite rule for nginx?

I'm trying to deploy trac with nginx. I almost have everything working exept for the rewrite rule for serving static files. I need to rewrite this url:
http://trac.domain.tldn/chrome/common/feed.png
to this one:
http://trac.domain.tldn/static/htdocs/common/feed.png
I have this code, but it isn't working:
location ~ /(.*?)/chrome/common/ {
rewrite /(.*?)/chrome/common/(.*) /$1/static/htdocs/common/$2 break;
root /var/www/domain.tldn/static/trac/static/htdocs/common;
}
Can you help me with this?
You can do it with the following code:
location /chrome/common {
rewrite ^/chrome/common/(.*) /static/htdocs/common/$1 permanent;
}
Or just use an alias for your files path:
location /chrome/common {
alias /var/www/domain.tldn/static/trac/static/htdocs/common;
}

Nginx redirect from one domain to another?

I'm on the process of migrating the same app but to a different domain.
For the old domain, I've the routes as:
http://app.example.com/app/users/sign_in?query=hello
I want it to be redirected to another domain omitting the app part as:
http://app.newexample.com/users/sign_in?query=hello
I tried with:
server {
...
location /app {
rewrite ^$scheme://app.sparkon.com/app(/.*)$ $1 last;
}
...
}
I doesn't work. How to achieve this?
I had this issue about a year ago and spent a long time looking for solutions. I found and use this:
server {
listen 80;
server_name example.com app.example.com;
rewrite ^/app(.*)$ http://app.newexample.com$1 permanent;
}
server {
listen 80;
server_name app.newexample.com;
# config for primary domain here
}
From this link. Credit to them.
Don't put scheme in the rewrite pattern:
server {
server_name app.example.com;
location /app/ {
rewrite ^/app/(.*)$ http://app.newexample.com/$1;
}
}
Brg.
I prefer this method as it doesn't need to use rewrite, one of the things that i read are good to avoid too much, cause it needs more processing by the nginx engine.
location ~ /app/(.*) {
return 301 $scheme://app.sparkon.com/$1;
}

Resources