Redirect pagination urls to parent - nginx

I am trying to redirect URLs ending in /whatever/page/X to /whatever
Here is what I have so far, but it redirects to the home page:
rewrite /page/([0-9]+)/$ /$1 redirect;
This is directly in my server block. Mostly written from what I can piece together, I'm not great with Nginx redirects.

Just found it:
rewrite /(.+)/page/([0-9]+)/$ /$1 redirect;

Related

Nginx rewrite rule not works when using proxy_pass

I am using nginx, and is configured to proxy_pass the requests
proxy_pass http://127.0.0.1:3000/;
So when user asks for
https://example.com/salon/1234
every thing works fine.
Now I need this URL to works either
https://example.com/salon/1234/anystringt
So I wrote this
rewrite ^(.*)/salon/([0-9].*)/(.+)$ /salon/$2 last;
and it does hot work.
When I change the rewrite rule to
rewrite ^(.*)/salon/([0-9].*)/(.+)$ /salon/$2 redirect;
everything works fine but it redirecting the user.
Any one knows why the first rewrite rule does not work?

nginx how to redirect all pagination

I need help with my nginx configuration.
The goal is to have all requests to my site like site/page1 site/smth/page1 be redirected to just site/ and site/smth/ basically for all requests ending like page[number]
I tried some examples that I found like rewrite ^/page/(.*)$ /$1; still wasn't able to get the redirection. Maybe I misplaced it, not quite sure where I should put the sting. Tried location and server blocks.
The nginx documentation examples for redirecting were a bit too hard to understand for me, so a little explanation would be great.
If you need a 301 HTTP redirection, try this rewrite rule (before the first location block):
rewrite ^(.*/)page\d+$ $1 permanent;
You can try something like this (not tested)
location ~ ^/(.+)/page[0-9]+$ {
rewrite ^/(.+)/page[0-9]+$ /$1 last;
}

Nginx rewrite - swap path in URL

Please help me with nginx:
url www.server.com/products/customer/books
should be www.server.com/customer/products/books
So i need to swap /products/ with /customer/
Yes, you can do this with a rewrite. Add the following to your server block:
rewrite ^/customer/products/(.*)$ /products/customer/$1 last;
Or, if you have multiple URLs (ie customer & business), you can do this:
rewrite ^/(customer|business)/products/(.*)$ /products/$1/$2 last;

nginx URL rewrites, applying wildcard

I am using nginx to rewrite some urls. how would I take the wildcard part of the initial url and apply it to the destination url?
ex. ^/il-it/news /news where news is its the wildcard and its auto applied to the destination.
rewrite ^/il-it(/.*)?$ / permanent;
Try this -
rewrite ^/il-it/(.+)/?$ /$1 permanent;

Nginx rewrite simple issue

I've migrated my site and now got a huge list of links that I need to redirect to their corresponding new urls. I'm not a developer or sys admin and can't really help myself with the Nginx manuals.
A short snippet of my list looks like this:
rewrite /?tn=productview&c=34223&pid=8965214 /url1 permanent;
rewrite /?tn=productview&c=54424&pid=6180497 /url2 permanent;
rewrite /?tn=productview&c=54426&pid=2563446 /url3 permanent;
rewrite /?tn=productview&c=54426&pid=6889674 /url4 permanent;
The problem is that Nginx will in that particular case redirect the first line correctly and all following lines to "url1".
How should I write this so that each of this links be redirected to their corresponding new url?

Resources