Nginx rewrite simple issue - nginx

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?

Related

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

Redirect pagination urls to parent

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;

nginx rewrite middle of url to another pattern

This nginx rule works great for me for a full specified file path
rewrite ^/sitemap.xml$ /sitemap.php last;
When I acces sitemap.xml it works as expected but in the background sitemap.php is requested. So far so goode.
Another problem arised and I need to rewrite the last part of existing urls
rewrite ^doctor-solution.html/ doctor-answer.html/ permanent;
What I want to achive is when an old url like
https://example.com/case12232-doctor-solution.html/ is accessed
it must be redirected to
https://example.com/case12232-doctor-answer.html/
But My rule doesn't seem to work. Any ideas?

Rewrite old image url with nginx

I have a single image that I renamed and need to redirect from the old image url to the new one my server uses nginx, I'm not having much success with the following rewrite:
rewrite ^/assets/avatar/avatar.png /assets/avatar/newavatar.png permanent;
Does someone know what is wrong?
If you want to use rewrite you need to use a full path to the new location
rewrite ^/assets/avatar/avatar.png http://example.com/assets/avatar/newavatar.png permanent;
A better way though is using a return
location /assets/avatar/avatar.png {
return 301 $scheme://example.com/assets/avatar/newavatar.png;
}
If you want to just rewrite, you need to change permanent to last or break

remove /profile/ from a link

On my site my users click on a profile and the link shows up as www.website.com/profile/username and I would like it to only be www.website.com/username - any suggestions on how I can do this
Note: my webserver is nginx so using rewrite rules is ok, but I would want to know how to write them in the config file
Use the following rewrite rule:
rewrite ^/profile/(.*)$ /$1 permanent;

Resources