nginx rewrite subfolder and file extension from the url - nginx

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.

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?

Multiple Nginx rewrites are not working properly

This is a rewrite for processing GET requests as subdirectories
rewrite ([A-Za-z0-9-]+)/([A-Za-z0-9-_]+)$ /__api/$1.php?r=$2 last;
rewrite ([A-Za-z0-9-]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)$ /__api/$1.php?r=$2&o=$3 last;
hello-world/var1_test
to
/__api/hello-world.php?r=var1_test
-> Working properly
hello-world/var1_test/var2_test
to
/__api/hello-world.php?r=var1_test&o=var2_test
->
Returns 404
Log shows that this request is actually accessing to hello-world/var1_test/var2_test. In short, rewrites are not working.
But it seems first rewrite is working properly while second rewrite is not working.
Is there anything wrong on the code?
EDIT:switching the line will make the rewrite passage on the first line able to use. but not the second line
Solved by myself.
It was about the directory misconfiguration which I forgot the start and the end of the URI. by using ^ and $ in the regular expression

nginx rewrite version assets

Help me please. I use nginx and my problem with rewrite.
I have in html code this link http://example.com/admin/assets/123/css/main.css
123 - it`s version of assets
How i can rewrite this path? to http://example.com/admin/assets/css/main.css
I try rule but not work
location ~ /assets/(.*)$ {
rewrite "/[0-9]{3}" /assets/$1 break;
}
You need to capture both parts of the URI, before and after the sequence you wish to delete.
rewrite "^(.*/assets/)[0-9]{3}/(.*)$" $1$2 last;
It is not necessary to place it inside a regular expression location block.

Nginx rewrite rule: Add subfolder to URI if not there

Hello I'm trying to write a Nginx rewrite rule that adds a subdirectory to my URL if it's missing. For example, I need http://example.com to be re-written (and redirected) to http://example.com/legacy-app. Can't seem to find the proper example to do this.
You can use a rewrite directive, but an exact match location block is most efficient:
location = / {
return 301 /legacy-app;
}
See this document for more.
We ended up using this:
rewrite ^/$ /legacy-app/ last;

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

Resources