Wordpress REST API (wp-api) 404 Error

非 Y 不嫁゛ 提交于 2019-11-27 19:19:54
Jitendra IT

UPDATED NEW WAY

I also faced similar problem in a local project. I used index.php after my project url and it worked.

http://localhost/myproject/index.php/wp-json/wp/v2/posts

If it displays a 404 error then update permalinks first (see "Paged Navigation Doesn't Work" section

If it works, maybe you need to enable mod_rewrite, on ubuntu:

a2enmod rewrite
sudo service apache2 restart

Installation

The REST API is included in WordPress 4.7! Plugins are no longer required, just install the latest version of WordPress and you're ready to go.

If you're before 4.7:

  1. Download plugin from here: http://v2.wp-api.org/

  2. install and activate it.

Usage

To get all posts:

www.mysite.com/wp-json/wp/v2/posts

For the search functionality, searching for test post looks like this:

/wp-json/wp/v2/posts?filter[s]=test

I had this problem with the latest WordPress 4.7+. In my case the REST API only worked after I changed the permalinks setting to something other than "Plain", which was the default setting for my installation.

On WPEngine and WP 4.9.2 I only had to update permalinks to get fresh, newly installed site to return v2 API calls. What I did:

  1. Create site
  2. Browse to http://yoursitename.wpengine.com/wp-json/wp/v2/posts
    • get 404
  3. Go to admin, settings, permalinks, choose "Post Name"
  4. Click "Save Changes"
  5. Browse to http://yoursitename.wpengine.com/wp-json/wp/v2/posts
    • success. page displays JSON response

It turned out to be a problem with the Apache configuration.

First, I deleted the .htaccess file in the root wordpress directory.

Next, I navigated to /etc/apache2/sites-enabled and opened 000-default

All of the AllowOverride variables were set to None, which I replaced with All.

That did the trick!

I had to manually make a .htaccess, set it to chmod 664, and copy the permalink rules into it.

I also played around with

  • Settings > Permalinks
  • Manually updating .htaccess via the code at the bottom of the permalinks page after clicked "Save"
  • Adding "index.php" as one of the other answers suggests
  • Making sure mod rewrite was enabled via a2enmod

I solved this issue through following steps:

  1. Navigate to ..\Apache24\conf\httpd.conf and search for LoadModule rewrite_module modules/mod_rewrite.so.

  2. Enable rewrite module by removing the # mark.

  3. Replace all the cases of AllowOverride None to AllowOverride All.

  4. Don't forget to restart apache server. :)

I had moved the WordPress install from a subdirectory to another, so in my case the problem was due to the WordPress config in the .htaccess files. It was trying to redirect every page but the homepage to the old directory. It was just a matter of updating olddir to newdir... This tripped me up more than once so I thought I'd put it here...

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /olddir/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /olddir/index.php [L]
</IfModule>

# END WordPress
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!