Nginx rewrite rule affects all subfolders - nginx

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

Related

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: 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 rewrite subfolder and file extension from the url

This is my scenario
https://x.com/remove/something/subfolder/sub.html
should be rewritten to
https://x.com/something/subfolder/sub
i.e I want to remove a URL part (remove in this case) and the file extension .html
so far I've come with this
location /remove {
rewrite ^/remove(/.*)$ $1 last;
}
but it's not working
UPDATE
got remove part solved by
rewrite ^/remove/(.*)$ https://x.com/$1 last;
now just need the file extension removal only
Your regular expression is capturing everything after the /remove part. You need to take the suffix out of the capture.
rewrite ^/remove(/.*)\.html$ $1 permanent;
See this document for details. Also, note use of the appropriate flag.

Nginx Path with Multiple Masks

I have a url directory path localhost/storage/app/media
I need it to be masked with both localhost/v/ and localhost/i/.
Nginx Sites-Available
This works for /v/, but if I make the same rules with /i/ it conflicts and shows a blank page with the message File not found. on all pages of the site.
# Mask storage media directory with /v/
rewrite ^/v(.*)$ /storage/app/media$1 last;
# Mask storage media directory with /i/
rewrite ^/i(.*)$ /storage/app/media$1 last;
You need to make your regular expression more specific. At the moment you are matching anything that begins with an i, for example, index.html. Try:
rewrite ^/v(/.*)$ /storage/app/media$1 last;
rewrite ^/i(/.*)$ /storage/app/media$1 last;

Nginx don't rewrite certain URL's

Right now I'm rewriting all http requests to Page.php with
rewrite ^ Page.php last;
But I want to allow to access file from /Ajax/ folder directly.
I tried adding (at the beginning)
rewrite /Ajax/SamplePage.php /Ajax/SamplePage.php last; (for example)
But still landed on Page.php
Is there any way to write something like
rewrite ^/Ajax/ - last;
?
You can try:
rewrite ^(?!(/Ajax/))(.*)$ /Page.php last;

Resources