Nginx server not working with trailing slash - nginx

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.

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?

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;

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?

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

Categories

Resources