Wordpress REST API(wp-api)404错误

时间:2016-01-08 06:08:21

标签: wordpress wordpress-plugin wp-api wordpress-rest-api

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

  

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

启用了漂亮的永久链接,使用以下结构http://.../wordpress/sample-post/,在导航到浏览器中的特定帖子时效果很好。

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

  • Wordpress 4.4.1
    • 不是多站点
  • WP REST API插件2.0-beta9
  • Apache 2.2.22
  • Ubuntu 12.04.5

任何帮助都会非常感激,因为我经历了几个小时的SO和WP支持论坛并且没有想法。谢谢!

15 个答案:

答案 0 :(得分:72)

更新新途径

我在本地项目中也遇到过类似的问题。 我在项目网址之后使用index.php并且它有效。

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

如果显示404错误,则update permalinks first (请参阅“分页导航不起作用”部分

如果有效,可能需要在ubuntu上启用mod_rewrite

a2enmod rewrite
sudo service apache2 restart

安装

  

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

如果您在4.7之前:

  1. 从此处下载插件:http://v2.wp-api.org/

  2. 安装并激活它。

  3. 用法

    获取所有帖子:

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

    对于搜索功能,搜索测试帖子如下所示:

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

答案 1 :(得分:20)

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

答案 2 :(得分:14)

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

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

答案 3 :(得分:13)

结果证明Apache配置存在问题。

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

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

所有AllowOverride变量都设置为无,我将其替换为All

这就是诀窍!

答案 4 :(得分:2)

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

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

答案 5 :(得分:1)

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

我也玩过

  • 设置>固定链接
  • 点击"保存"
  • 后,通过永久链接页面底部的代码手动更新.htaccess
  • 添加" index.php"正如其他答案之一所暗示的那样
  • 确保通过mod rewrite
  • 启用a2enmod

答案 6 :(得分:1)

我通过以下步骤解决了这个问题:

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

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

  3. AllowOverride None的所有情况替换为AllowOverride All

  4. 别忘了重新启动apache服务器。 :)

答案 7 :(得分:1)

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

这是解决方法How to deal with GCP WordPress error "This page isn’t working example.com is currently unable to handle this request. HTTP ERROR 500

答案 8 :(得分:0)

我已经将WordPress安装从一个子目录移动到了另一个目录,因此在我的情况下,问题是由于.htaccess文件中的WordPress配置引起的。它试图将除主页以外的所有页面重定向到旧目录。只需将olddir更新为newdir ... 这使我不止一次绊倒,所以我以为我会把它放在这里...

# 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

答案 9 :(得分:0)

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

答案是最近对我的服务器运行方式进行了更改。这破坏了REST API,但直到后来才变得明显。

我已经将该域从使用Apache更改为使用nginx,并且我没有正确转移.htaccess定制。因此,此问题的快速解决方案是改回使用Apache。这将使站点立即恢复工作状态。

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

答案 10 :(得分:0)

我在本地主机上也遇到了同样的问题,我只需在.htaccess文件中设置 RewriteBase 路径即可解决此问题,该路径位于WordPress项目设置的根文件夹中。

<Type Name="System.Text.Json.Serialization.Converters.JsonConverterString" Dynamic="Required All" />
<Type Name="System.Text.Json.Serialization.Converters.JsonConverterUri" Dynamic="Required All" />
<Type Name="System.Text.Json.Serialization.Converters.JsonConverterBoolean" Dynamic="Required All" />
<Type Name="System.Text.Json.Serialization.Converters.JsonConverterInt32" Dynamic="Required All" />
<Type Name="System.Text.Json.Serialization.Converters.JsonConverterDouble" Dynamic="Required All" />
<Type Name="System.Text.Json.Serialization.Converters.JsonConverterInt16" Dynamic="Required All" />
<Type Name="System.Text.Json.Serialization.Converters.JsonConverterByte" Dynamic="Required All" />
<Type Name="System.Text.Json.Serialization.Converters.JsonConverterDecimal" Dynamic="Required All" />
<Type Name="System.Text.Json.Serialization.Converters.JsonConverterGuid" Dynamic="Required All" />
<Type Name="System.Text.Json.Serialization.Converters.JsonConverterEnum" Dynamic="Required All" />
<Type Name="System.Text.Json.Serialization.Converters.JsonConverterDateTime" Dynamic="Required All" />
<Type Name="System.Text.Json.Serialization.Converters.JsonConverterDateTimeOffset" Dynamic="Required All" />

答案 11 :(得分:0)

如果您尝试了此页面上的其他解决方案但它们没有奏效,我已成功检查了 public_html 文件夹(或您安装 WordPress 的任何地方)上方的根文件夹中的其他 .htaccess 文件。

我发现了一个额外的,它可能来自以前的安装或意外移动到那里 - 它向“真实的”.htaccess 文件提供了相互冲突的指令。删除它为我解决了问题。

答案 12 :(得分:0)

通过 SSH 向我的 Ubuntu 服务器上的 apache 虚拟主机配置添加“AllowOverride All”(如其他作者之前所暗示的)对我来说是个窍门:

<?php

namespace Acme;

const LIBRARY_DIR = __DIR__.'/lib'; // Where our classes reside

/**
 * Autoload classes within the current namespace
 */
spl_autoload_register(function($qualified_class_name) {

    $filepath = str_replace(

        '\\', // Replace all namespace separators...
        '/',  // ...with their file system equivalents
        LIBRARY_DIR."/{$qualified_class_name}.php"
    );

    if (is_file($filepath)) {

        require_once $filepath;
    }
});

new Foo();
new Bar\Bar();

还有(如果你使用letsencrypt):

sudo vi /etc/apache2/sites-available/my-website-name.com.conf

文件应如下所示:

sudo vi /etc/apache2/sites-available/my-website-name.com-le-ssl.conf

不要忘记禁用并重新启用站点并重新加载apache以应用新配置:

<VirtualHost *:80>
# or <VirtualHost *:443> for the SSL configuration
    
    # [...]
    
    DocumentRoot /var/www/my-website-name.com/public_html

    <Directory "/var/www/my-website-name.com/public_html">
        # this allows .htaccess files (e.g. generated by Wordpress)
        # to overwrite the apache configuration on a directory basis:
        AllowOverride All
    </Directory>
    
    # [...]
    
</VirtualHost>

答案 13 :(得分:0)

首先,您必须检查 WordPress REST API 是否已启用

最好的检查方法是访问此 URL:https://yoursite.com/wp-json。 如果您看到一些 JSON 响应,则 REST API 已启用。 如果它显示一些错误页面或返回主页,则未启用 REST API。然后我们必须先启用它。

在这种情况下,您必须启用永久链接

  1. 访问您页面的管理页面(访问 https://yoursite.com/wp-json
  2. 设置 > 固定链接(访问 https://yoursite.com/wp-admin/options-permalink.php
  3. 确保未选择普通
  4. 选择帖子名称(最好的)
  5. 然后点击保存更改。这将重写/修复您的 .htaccess

请看下面的帮助截图:

Permalinks

choosing Post Name

Source

答案 14 :(得分:0)

例如,如果您的网站是 https://example.wordpress.com(在 wordpress 上),请使用以下链接,无论任何 API 设置/固定链接等如何,都会为您提供 JSON 响应。

如果您想查看所有可用的端点,请使用此 - https://developer.wordpress.com/docs/api/console/

记得将 **$site** 替换为您的域

在此处查找最新文档 - https://developer.wordpress.com/docs/api/