Issue with redirect syntax in Nginx - 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;

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?

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.

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.

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 question

I'm new to nginx and I got a rewrite problem here:
I want to permanently redirect http://domain1.com/abc.php to http://domain2.com,
but I want to keep http://domain1.com/abc.php?param=value, I've tried put
rewrite ^/abc\.php$ http://domain2.com last;
which works for http://domain1.com/abc.php, unfortunately it rewrites everything that starts with the '/abc.php', I'm really confused why this is happening, any ideas?
Thanks in advance.
Nginx rewrites generally don't "see" the query string as part of the URI, which is why your existing rewrite isn't working - to Nginx it's always ^/abc\.php$ whether there's a query string or not.
Instead, I'd try this (adapted from the documentation):
if ($args !~ param=value) {
rewrite ^/abc\.php$ http://domain2.com permanent;
}
But be aware that if is evil.

Resources