3

When I access my WordPress site as www.example.com/wp-json/ I got this 404 error.

`{"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}`

But the REST api return the correct json object if I use the url of www.example.com/wp-json/wp/v2/.

My permalink is setup as /%year%/%monthnum%/%postname%/, and here is part of my nginx configuration settings:

server {

    root /var/www/html;
    index index.php index.html index.htm;

    location / {
      try_files $uri $uri/ /index.php$is_args$args;
    }

    # other location directives related to php, cache, etc.

}

I searched on Internet and most of the problems seems to be caused by incorrect permalink setting and .htaccess (Apache), but seldom mentioned about the case related to nginx. Any idea what cause this and how to solve it?

Update

If I run curl -i www.example.com/wp-json, this is what I get:

HTTP/1.1 404 Not Found
Server: nginx
Date: Sun, 29 Jan 2017 11:58:21 GMT
Content-Type: application/json; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
X-Robots-Tag: noindex
Link: <https://example.com/wp-json/>; rel="https://api.w.org/"
X-Content-Type-Options: nosniff
Access-Control-Expose-Headers: X-WP-Total, X-WP-TotalPages
Access-Control-Allow-Headers: Authorization, Content-Type

{"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}

Latest Update (7 Mar 2017)

With the release of WordPress 4.7.3, this bug has been fixed. The workaround unset ($_SERVER['PATH_INFO']); no longer needed.

7
  • What are you expecting to find at wp-json/? Jan 24 '17 at 6:42
  • Are you using a plugin to access the API? Jan 24 '17 at 6:45
  • When accessing wp-json/ I was expecting the same result as wp-json/wp/v2, this was the correct behaviour on my another server running Apache. No I don't have an plugin for REST. My WordPress is 4.7, I thought it is no longer require a plugin for REST API.
    – hcheung
    Jan 24 '17 at 7:51
  • It is no longer required to have a plugin, but it looks like core end points are the v2 version. Perhaps it is not Apache vs nginx, but you used a plugin previously? Jan 24 '17 at 8:02
  • No, I never used a plugin, as mentioned on my previous msg that for the same WordPress setting running on an Apache server works without a plugin.
    – hcheung
    Jan 24 '17 at 11:26
1

Could this be about https://core.trac.wordpress.org/ticket/39432

Symptoms look very similar and at the time of writing hasn't been released as 4.7.3. Applying the patch manually fixed the issue on my nginx setup.

2
  • Thanks for the link. This does fixed the problem, but since it involving of modifying the code in /wp-includes, I'd prefer not to change it this way. I'm currently on 4.7.2, hope this will be fixed in upcoming 4.7.3 release. In the meantime, I follow @user113522 instruction by unsetting $_SERVER['PATH_INFO'].
    – hcheung
    Feb 22 '17 at 14:59
  • Modifying core specifically for this patch doesn't really make a difference, since the update overwriting everything will contain the same thing.
    – lkraav
    Feb 22 '17 at 18:30
2

Just ran into the same issue on an nginx only (no Apache) WordPress blank install 4.7.2 site.

The base /wp-json/ & index.php?rest_route=/ URLs showing rest_no_route 404, but all the /wp-json/wp/v2/ working just fine.

The issue turned out to be related to the PATH_INFO variable passed by nginx that WordPress core attempts to build the URL off of incorrectly, if it's set to anything, even an empty string.

Needs more investigating, but I was able to fix by adding a condition on those specific pages via REQUEST_URI by doing this for them:

unset($_SERVER['PATH_INFO']);
1
  • Ah, thank you @user113522, I know why this happened now. WordPress really on .htaccess to handle the redirect, but in case of Nginx, Nginx does not use .htaccess, when WordPress can't find the .htaccess, it fallback to use the PATH_INFO which add an extra index.php into the url. Your solution of adding unset($_SERVER['PATH_INFO']); works but I think it is better to setup a redirect at the nginx location directive. I will test it when I have time and post it here later.
    – hcheung
    Feb 22 '17 at 6:19
1

For me, updating the permalinks to something special than the first option did the job.

1

It worked for me with the proposed nginx setup from wordpress. add this to your nginx.conf

location /wp-json/ { # Resolves WP Gutenberg 404 issue
  try_files $uri $uri/ /index.php;
} 

and change the 'Permalinks' settings to 'Post name'

this should work with /wp-json issue. https://wordpress.org/support/article/nginx/

I configured a network site with the instructions given and it all seemed to work well. Worth giving it a try!

Also, https sites must have right permalinks, just check that one as well!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.