Wordpress REST API (wp-api) 404 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34670533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:11:23  来源:igfitidea点击:

Wordpress REST API (wp-api) 404 Error

wordpresswordpress-pluginwp-apiwordpress-rest-api

提问by dsal1951

I have been using the Wordpress REST plugin WP-API for months now while developing locally with XAMPP. I recently migrated my site to an EC2 instance and everything is working fine exceptI now get a 404 with the following message whenever I try to access any endpoint on the API:

在使用 XAMPP 进行本地开发时,我已经使用 Wordpress REST 插件 WP-API 几个月了。我最近将我的站点迁移到了一个 EC2 实例,一切正常,现在每当我尝试访问 API 上的任何端点时都会收到带有以下消息的 404:

The requested URL /wordpress/wp-json/ was not found on this server

在此服务器上找不到请求的 URL /wordpress/wp-json/

Pretty permalinks are enabledwith the following structure http://.../wordpress/sample-post/which works fine when navigating to a specific post in the browser.

使用以下结构启用漂亮的永久链接http://.../wordpress/sample-post/,在浏览器中导航到特定帖子时可以正常工作。

Here are some details about my setup:

以下是有关我的设置的一些详细信息:

  • Wordpress 4.4.1
    • Not a Multisite
  • WP REST API plugin 2.0-beta9
  • Apache 2.2.22
  • Ubuntu 12.04.5
  • WordPress 4.4.1
    • 不是多站点
  • WP REST API 插件 2.0-beta9
  • 阿帕奇 2.2.22
  • Ubuntu 12.04.5

Any help would be greatly appreciated as I have gone through SO and the WP Support forums for several hours and am out of ideas. Thank you!

任何帮助将不胜感激,因为我已经浏览了 SO 和 WP 支持论坛几个小时并且我没有想法。谢谢!

回答by Jitendra Popat

UPDATED NEW WAY

更新的新方式

I also faced similar problem in a local project. I used index.phpafter my project urland it worked.

我在本地项目中也遇到过类似的问题。index.php在我的项目 url 之后使用,它工作正常。

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

如果它显示 404 错误,则首先更新永久链接(请参阅“分页导航不起作用”部分

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

如果有效,也许您需要mod_rewrite在 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.

REST API 包含在 WordPress 4.7 中!不再需要插件,只需安装最新版本的 WordPress 即可。

If you're before 4.7:

如果您在 4.7 之前:

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

  2. install and activate it.

  1. 从这里下载插件:http: //v2.wp-api.org/

  2. 安装并激活它。

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

回答by dlaub3

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.

我在最新的 WordPress 4.7+ 上遇到了这个问题。在我的情况下,REST API 仅在我将永久链接设置更改为“Plain”以外的设置后才起作用,这是我安装的默认设置。

回答by Andy Roberts

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:

在 WPEngine 和 WP 4.9.2 上,我只需要更新永久链接即可获得全新的、新安装的站点以返回 v2 API 调用。我做了什么:

  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
  1. 创建站点
  2. 浏览到http://yoursitename.wpengine.com/wp-json/wp/v2/posts
    • 得到 404
  3. 转到管理,设置,永久链接,选择“帖子名称”
  4. 点击“保存更改”
  5. 浏览到http://yoursitename.wpengine.com/wp-json/wp/v2/posts
    • 成功。页面显示 JSON 响应

回答by dsal1951

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

原来是Apache配置的问题。

First, I deleted the .htaccessfile in the root wordpress directory.

首先,我删除了.htaccesswordpress 根目录中的文件。

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

接下来,我导航到/etc/apache2/sites-enabled并打开000-default

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

所有AllowOverride变量都设置为 None,我用All.

That did the trick!

成功了!

回答by faryal rida

It is the file permission error, apply the following solution:

这是文件权限错误,应用以下解决方案

Edit this file /etc/apache2/apache2.confChange /var/www/Permissions from "None" to "All" Restart the apache2server.

编辑此文件 将权限从“无”/etc/apache2/apache2.conf更改/var/www/为“全部” 重新启动apache2服务器。

回答by Hymansonkr

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

我必须手动制作一个.htaccess,将其设置为chmod 664,然后将永久链接规则复制到其中。

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 rewritewas enabled via a2enmod
  • 设置 > 固定链接
  • 单击“保存”后,通过永久链接页面底部的代码手动更新 .htaccess
  • 添加“index.php”作为其他答案之一建议
  • 确保mod rewrite通过启用a2enmod

回答by Patriotic

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 Noneto AllowOverride All.

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

  1. 导航到 ..\Apache24\conf\httpd.conf 并搜索LoadModule rewrite_module modules/mod_rewrite.so.

  2. 通过删除#标记启用重写模块。

  3. 替换AllowOverride Noneto 的所有情况AllowOverride All

  4. 不要忘记重新启动apache服务器。:)

回答by Pinkeye

I found that mysite/wp-json/was not working, but mysite/?rest_route=/was normal. This was breaking some, but not all, the REST API features used on my site.

我发现这mysite/wp-json/不起作用,但mysite/?rest_route=/很正常。这破坏了我网站上使用的部分(但不是全部)REST API 功能。

The answer to this turned out to be a recent change to how I was running my server. This had broken REST API but this was not apparent until later.

事实证明,这个问题的答案是最近对我运行服务器的方式所做的更改。这已经破坏了 REST API,但这直到后来才显现出来。

I had changed this domain from using Apache to using nginx, and I had not correctly transferred the .htaccesscustomisations. The quick solution to this problem was therefore to change back to using Apache. This restored the site to working order immediately.

我已将此域从使用 Apache 更改为使用 nginx,但我没有正确传输.htaccess自定义。因此,此问题的快速解决方案是改回使用 Apache。这立即使站点恢复了工作状态。

I will be changing this domain back to nginx in the future but when I do, I will test it and be careful not to affect the REST API.

将来我会将此域改回 nginx,但是当我这样做时,我会对其进行测试并注意不要影响 REST API。

回答by Zulkifl Agha

I faced the same problem when I migrated my site from cPanel to Google Cloud Compute Engine Instance; problem was of file permissions which was initially caused due to difference in PHP versions of current deployment from previous deployment.

当我将我的网站从 cPanel 迁移到 Google Cloud Compute Engine 实例时,我遇到了同样的问题;问题是文件权限问题,最初是由于当前部署的 PHP 版本与以前的部署不同造成的。

Here is the fix How to deal with GCP WordPress error "This page isn't working example.com is currently unable to handle this request. HTTP ERROR 500

这是修复如何处理 GCP WordPress 错误“此页面不起作用 example.com 当前无法处理此请求。HTTP 错误 500

回答by mrtnmgs

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 .htaccessfiles. It was trying to redirect every page but the homepage to the old directory. It was just a matter of updating olddirto newdir... This tripped me up more than once so I thought I'd put it here...

我已经将 WordPress 安装从一个子目录移动到另一个子目录,所以在我的情况下,问题是由于.htaccess文件中的 WordPress 配置。它试图将除主页之外的每个页面重定向到旧目录。这只是更新olddirnewdir......这让我不止一次绊倒,所以我想我会把它放在这里......

# 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