Nginx 301 Redirect for folders - nginx

I am moving to a new site and the folder path has slightly changed.
Old Path: /forms/somepdf.pdf
New Path: /uploads/forms/somepdf.pdf
Note that there are sub folders in the forms folder that contain pdf documents as well. I also have an html page in /forms. So normally this would work:
rewrite ^/(forms.*) /uploads/$1 permanent;
But this makes my forms html page error 404. So, I only want to do the redirect if the filename ends with .pdf and the requested URL starts with "/forms". This is what I have so far, but it is not quite working:
rewrite ^/(forms.*)\.pdf /uploads/$1/\.pdf last;

Actually, I played with it a little more and this seems to work:
rewrite ^/(forms.*)\.pdf /uploads/$1.pdf permanent;
If you have a better solution please let me know. Thanks

Related

NGINX rewrite with url paramter

I want to user more readable urls.
I have this url, that works and want to use shorter url
current: https://www.xxxxxxxxx.de/blog/post?title=ooono
wanted: https://www.xxxxxxxxx.de/blog/ooono
For that, i putted this code in my nginx lines:
rewrite ^/blog/(\d+)$ /blog/post?title=$1 last;
but for some reason, it sends me to 404 page error_page 404 /404.php; and it doesnt work
Did someone has any ideas?
I tried different rewrite rules, trying to catch the error but dont have anything in my nginx log
fyi:
/blog is a folder, inside is the post.php file
probably thats the problem?

WP migration from apache to nginx results -- 404 -- on https

That was kind of a loaded title.
I moved my site over from Apache to Nginx and I'm learning the hard way that Nginx does things its own ways. It does not like htaccess files, ignores them. This causes problems when running Wordpress, because wordpress supposedly loves to use htaccess files and nginx does not. Why? I have no idea.
Anyways,
I managed to figure out how to bring back the site from the abyss of 404, by placing this code in nginx.conf file
location / {
index index.php index.html;
if (!-e $request_filename)
{
rewrite ^/(.+)$ /index.php last;
}
}
But.
while pages load fine on HTTP, HTTPS still shows dreaded 404. Why? I don't know. So, anybody know what to do next?
Well, it turns out I also have to add the same code on nginx.ssl.conf file.
Why? I don't know, but it works.

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?

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?

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

Resources