Redirecting old url to Nginx with param - nginx

I have an old URL structure like this archives.php?l=e and I moved it to a new structure archives/e
I'm trying to 301 redirect all the old urls, but I have run into troubles. This is what I'm currently doing but it's not working.
location ^~ /list.php {
rewrite /archives/$1/ permanent;
}
Any help would be greatly appreciated.

I figured it out and I have to say Nginx has some nice magic for this. Using arg_l solved it. Somehow it creates a variable for each param.
location ^~ /archives.php {
rewrite ^/archives.php$ /archives/$arg_l? permanent;
}

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

Rewrite a URL in nginx without redirecting

I'm trying to create a rewrite with nginx that will change the displayed URL without actually redirecting to it. EG: http://example.com/item/rest-of-path would become http://example.com/folder/rest-of-path. I've been working with different variations of this code with in my nginx.conf:
location ~ /example {
rewrite ^/example/(.*) /folder/$1 last;
}
but that doesn't seem to be doing the trick. Any ideas where I'm going wrong here? I'll admit I'm still pretty new to server-side rewrites in general.
Try this:
location ~ /example {
rewrite ^/example/(.*) /folder/$1 break;
}
use break.
Try this. It isn't necessary to place it under the location block, and don't forget to reload nginx.
rewrite ^/example/(.+)$ /folder/$1 last;

Redirect a specific URL to another in NGINX

I need to redirect a specific url containing a parameter and an anchor to a new URL:
/pages/page.php3?page=fond_razeni_gk.htm#GKI
to
http://www.newserver.com/o-knihovne
I try:
rewrite ^/pages/page.php3\?page=fond_razeni_gk.htm$ http://www.newserver.com/o-knihovne? redirect;
but this does not work. Could you please advise me what is wrong?
try this configuration:
location / {
rewrite ^/pages/page.php3(.*)$ http://www.newserver.com/o-knihovne$1 permanent;
}

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

nginx 301 rewrite root domain works but files are not being redirected

I'm trying to redirect a domain to another url in nginx and it works partially.
It looks like this:
location / {
rewrite ^/(.*) http://www.some_domain.tld/some_dir/some_file.php permanent;
}
It redirects fine the root domain, however if I try to load http://www.my_old_domain.tld/some_file.php it won't do the same. It'll load the page.
Thoughts ?
Thanks.
Try using this
location /
return 301 http://newdomain.com/a/b/c.php;
}

Resources