NGINX rewrite with url paramter - nginx

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?

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;

How do I configure nginx for WordPress REST API in sub-folder?

I am trying to set up multiple Wordpress sites in sub-folders under our domain (ie not multi-site), but I have difficulty configuring the REST API endpoints. For example, this endpoint works fine:
https://example.com/site1/?rest_route=/wp/v2/posts
But this endpoint gives a 404:
https://example.com/site1/wp-json/wp/v2/posts
I have tried to rewrite the failing url to the working url with these rules in my nginx configuration:
location /site1/wp-json {
rewrite ^/site1/wp-json(.*)$ /site1/?rest_route=$1;
}
location /site1/ {
try_files $uri $uri/ /site1/index.php$is_args$args;
}
I can't see any special handling of wp-json in the WordPress docs or the nginx wiki. What am I missing here? The permalinks for the site is set to Numeric (https://example.com/site1/archives/123) if that might play a role.
Update
Gist of the redacted full config file and the config syntax lints okay:
nginx -c /etc/nginx/nginx.conf -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
I just hit this too, in WP 5.7. Without pretty permalinks, ie with the "Plain" option like ?p=123, my nginx WP installation uses requests like:
/index.php?rest_route=/wp/v2/users/&who=authors...
And these all work fine.
However if I enable pretty permalinks, eg "Post name", /sample-post/, it starts making requests like:
/wp-json/wp/v2/users/?who=authors...
And these all return a 404. For example, editing or publishing posts fails, and browser devtools shows a string of 404s in this format.
But now we know the pattern that works, a solution is clear - we just need to map the not-working format to the working format:
# Resolves WP Gutenberg 404 issue
location /wp-json {
rewrite ^/wp-json(.*)$ /index.php?rest_route=$1 last;
}
I believe that the rewrite directive should be written as shown below:
server {
location /site1/wp-json
{
rewrite ^(/site1/wp-json.*)$ /site1/?rest_route=$1 last;
}
}
I was able to resolve it like this:
location /wordpress/ {
rewrite ^/wordpress/wp-json/(.*?)$ /wordpress/index.php?rest_route=/$1 last;
}
An easy way if your website pages in the subfolder is already working, just add index.php to the url:
https://site1.com/site2/index.php/wp-json/
If your website pages still doesn't work in the subfolder, add this code to nginx/sites-available/website.conf file too:
location /site2 {
rewrite ^(/[^/]+)?(/wp-.*) /site2/$2 break;
rewrite ^/site2/(.*)$ /site2/index.php?q=$1 last;
}

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?

Resources