Nginx Redirects - nginx

I'm struggling with a nginx redirect that I've set up.
Basically, I want to redirect /case-study/ to /case-studies/ but don't want to redirect any url past /case-study/*.
I have this setup already:
location = /case-study/ {
rewrite ^(.*)$ /case-studies/ permanent;
}
Which works for /case-study/ but also redirects any url after /case-study/test-url-redirect
Cany anyone help me out on this one.

The simplest way is to make it a regex location like so:
location ~ /case-study(/?)$ {
return 301 /case-studies/;
}
Should match /case-study and /case-study/ and redirect to /case-studies/ but not redirect anything that is under case-study

Related

Rewrite Nginx Url only on specific path

So I have a main path https://app.example.com and I want it to keep it that, way but on all https://app.expample.com/p/* paths I want it to be rewritten to a short version of the url like. How can I achieve this?
I tried
location = /p/(.*)$ {
rewrite ^ https://example.com/p/$request_uri permanent; and also return 301 https://example.com/p/$request_uri;
}
but this resulted in too many http redirects and an error

Nginx redirect being ignored

I'm trying to create a redirect for all URLs on a domain that contains /comfy/ to another domain, but keeping the rest of the request.
For example
https://www.example.org/system/comfy/cms/files/files/000/002/012/profile/AdobeStock_194204879.jpeg
to
https://www2.example.org/system/comfy/cms/files/files/000/002/012/profile/AdobeStock_194204879.jpeg
In nginx in the server block for example.org I have
location ~ ^/comfy/(.*) {
return 301 $scheme://www2.example.org/$request_uri;
}
but i'm still getting a 404, with the original domain, ie it's still accessing
https://www.example.org/system/comfy/cms/files/files/000/002/012/profile/AdobeStock_194204879.jpeg
I have also tried
rewrite ^(/comfy/)(.*)$ http://www2.example.org/$2 permanent;
and
rewrite ^/comfy/(.*) http://www2.example.org/$1 break;
but these are failing too. What am I missing?
Thanks!
Ok, turns out I needed to do
location ~ ^/system/comfy/cms/(.*) {
return 301 $scheme://www2.example.org/$request_uri;
}

Redirecting old url to Nginx with param

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

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

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