Nginx rewrite URL, change parts of URL - nginx

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;
}

Related

How I can rewrite a url that contain a date on NGINX

I have a problem with a rewrite url on wordpress.
The url looks like this: example.com/blog/2020-12
I would like to turn it into: example.com/blog/2020/12
I tried this unsuccessfully:
location ~ "^/blog/([0-9]{4}+)-([0-9]{2}+)/$" {
rewrite ^(.*)-(.*) $1/$2 permanent;
}

What is the syntax for a nginx url rewrite

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;

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;
}

Rewrite nginx cut last location

I would like to rewrite the following URL http://example.net/bar/foo to http://example.net/bar.
Do you know how to do this ?
Use this :
location /bar {
rewrite ^/bar/(.*)/[^/]+$ /bar/$1;
}

how do I rewrite a URL in nginx to an updated location?

I have the following URLs:
1) http://example.com/downloads/
2) http://example.com/downloads/widgets
3) http://example.com/downloads/gadgets
These URLs need to be redirected rewritten to the following:
1) http://example.com/products/
2 & 3 & etc) http://example.com/products/thingies
I'm currently trying the following nginx code:
location ~* ^/downloads/ {
rewrite ^/downloads/$ /products break;
rewrite ^/downloads/(.*)$ /products/thingies break;
}
It's almost working, however my site's document root is /var/www/example.com/public. So after processing the rewrite rules, nginx tries to literally serve /var/www/example.com/public/products/, whereas I want it to just rewrite to http://example.com/products/ (which then proxies to PHP and so on).
Where am I failing? Is there a different, better way to accomplish this?
Thank you for any help.
-- UPDATE --
I got it to work by using the following rules:
rewrite ^/downloads/?$ $scheme://$host/tools last;
location ~* ^/downloads/ {
rewrite ^/downloads/?$ $scheme://$host/products last;
rewrite ^/downloads/(.*)$ $scheme://$host/products/thingies last;
}
IS this the proper way of doing it in nginx? I haven't seen this rewrite rule format anywhere while researching this. It somehow seems odd.
Your update redirects, not rewrites.
Here is how I would do:
location /downloads/ {
rewrite ^ /products/thingies;
}
location = /downloads/ {
rewrite ^ /products/;
}
# uncomment if you need '/downloads' (without trailing slash) act as '/downloads/'
#location = /downloads {
# rewrite ^ /products/;
#}
The correct syntax appears to be:
location ~* ^/downloads/ {
rewrite ^/downloads/?$ /products permanent;
rewrite ^/downloads/(.*)$ /products/thingies permanent;
}

Resources