Multiple Nginx rewrites are not working properly - nginx

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

Related

Nginx server not working with trailing slash

I have setup Ubuntu 20.4 with Nginx.
All working fine except following
When someone hit https://www.example.com/webservice.php all working fine as expected.
but If tried to hit https://www.example.com/webservice.php/ then it gives 404 error.
here, It just have extra slash at end.
I already tried to update Nginx default file as follow with rewrite rules
rewrite ^/(.*)/$ /$1 permanent;
However it redirect to webservice.php from webservice.php/
But it just result empty page while it rewriting it doesn't pass it request body there.
Please, any solution?
By adding the slash, you are saying it's a URI pointing at a directory and not the PHP file. There is no directory called webservice.php I presume. Only a file. Therefore, the 404 error. Don't do that.

Issue with redirect syntax in Nginx

I get an error with redirects in my conf file in nginx. Before I used this syntax:
location = /old-url/ {
return 301 /new-url/;
}
This was no longer working so I adopted the following:
rewrite ^/old-url/$
/new-url/ permanent;
The second one seemed to be working fine, but now I get an error:
This page isn’t working www.my-website.co.uk redirected you too many
times. Try clearing your cookies. ERR_TOO_MANY_REDIRECTS
I am new to nginx and not sure which syntax is correct and why the second one is no longer working.
Try with below currently I didn’t ran it on my side,
rewrite ^/old-url/ http://your-site.com/new-url/ permanent;

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 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 Rewrite Not rewriting the url

I have tried this for hours and can't seem to get it. I copied other examples and still can't get rewrite to work.
I need my url on nginx to look like http://myurl.com/main/login, http://myurl.com/somethigelse/home, est. Any Help Appreciated. I'm new to nginx, seems a lot faster.
My nginx rewrite looks like this:
rewrite ^/([^/]+)/([^/]+)$ /index.php?db=$1&action=$2 last;
rewrite ^/([^/]+)/([^/]+)/$ /index.php?db=$1&action=$2 last;
It works for me. Do you tell nginx to reload the configuration (do a sudo service nginx reload)? Otherwise, nginx will still be using the old config.
Note, that you can use one line by making the final slash optional using a question mark:
rewrite ^/([^/]+)/([^/]+)/?$ /index.php?db=$1&action=$2 last;
# ^

Resources