nginx rewrite middle of url to another pattern - nginx

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?

Related

Nginx: how to add a rewrite rule to redirect all the requests for a file type?

I would like to write a rewrite rule for Nginx to redirect any requests for javascript file to a specific JS file. For example 'http://example.com/path1/path2/sample.js?a=1&b=2' should be redirected to 'http://example.com/myjavascript.js'. So I added this rule to /etc/nginx/sites-enabled/example.com config file:
rewrite .*\.js.* myjavascript.js last;
so any URL containing .js should be replaced by myjavascript.js. But when I try the URL 'http://example.com/path1/path2/sample.js?a=1&b=2', it fails (404). However, the strange thing is that if I replace the js file with an HTML file, such :
rewrite .*\.js.* /hello.html last;
It redirects it correctly. myjavascript.js is in the same folder as hello.html, but it does not redirect it to this path correctly.
Can you please give me some idea of why it does not work?
The problem was the missing '/' before the javascript file name, so the correct one should be :
rewrite .*\.js.* /myjavascript.js last;

Nginx: redirect regardless of the url structure

I recently moved a subdomain to my main domain but I also changed the url structure.
Previously I had pages like http://sub.domain.com/companies/my-company-id/year/2012/charts
When moving to the main domain, I removed all the complicated urls to juts get:
http://www.domain.com/companies/my-company
I currently have the following rule:
rewrite ^/companies/(.*)$ http://www.domain.com/companies/$1 permanent; but when someone go on a page like http://sub.domain.com/companies/my-company/2012/charts they get redirect to http://www,.domain.com/companies/my-company/2012/charts and get a 404.
I like to force a redirection to http://www,.domain.com/companies/my-company-id regardless of what's after the my-company-id
Currently the parameter $1 is having the entire URI after /companies, so you are getting redirected to the original path. You should only extract the company-id in $1.
Use this:
rewrite ^/companies/(.*)/(.*)$ http://www.domain.com/companies/$1 permanent;
Here the rest of the URI after company-id will be available in the parameter $2, which is not needed in the rewrite condition.

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?

Nginx rewrite rule affects all subfolders

How can I change a rewrite rule so that it affects only folder 01:
/01/01.png
/02/02.png
If is used one of this three rewrite rules
rewrite /(.*(png))$ /01/$1 last;
rewrite /(.*)$ /01/$1 last;
rewrite ^ /01/$uri last;
the file /01/01.png can be opened directly through /01.png (that was the goal of the rewrite rule) but now the file /02/02.png can't be opened any more as usually, nginx shows only "404 Not Found". Also png files in all other subfolders can't be opened any more. How the rewrite rule must be changed so that it affects only folder 01? I'm new to nginx and can't find a solution, please can anyone help?
I have found a solution for the problem, now I use a second rewrite rule for other folders with jpg-files. Many Greetings

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