我一直在使用Wordpress REST插件WP-API几个月,而在本地使用XAMPP进行开发。我最近将我的网站迁移到EC2实例并且一切正常除了每当我尝试访问API上的任何端点时,我现在都会收到带有以下消息的404:
在此服务器上找不到请求的URL / wordpress / wp-json /
启用了漂亮的永久链接,使用以下结构http://.../wordpress/sample-post/
,在导航到浏览器中的特定帖子时效果很好。
以下是有关我的设置的一些详细信息:
任何帮助都会非常感激,因为我经历了几个小时的SO和WP支持论坛并且没有想法。谢谢!
答案 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之前:
从此处下载插件:http://v2.wp-api.org/
安装并激活它。
获取所有帖子:
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调用。我做了什么:
答案 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
,然后将永久链接规则复制到其中。
我也玩过
mod rewrite
a2enmod
答案 6 :(得分:1)
我通过以下步骤解决了这个问题:
导航到.. \ Apache24 \ conf \ httpd.conf并搜索LoadModule rewrite_module modules/mod_rewrite.so
。
通过删除#
标记来启用重写模块。
将AllowOverride None
的所有情况替换为AllowOverride All
。
别忘了重新启动apache服务器。 :)
答案 7 :(得分:1)
将网站从cPanel迁移到Google Cloud Compute Engine实例时,我遇到了同样的问题;问题是文件权限,最初是由于当前部署的PHP版本与以前的部署不同而引起的。
答案 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。然后我们必须先启用它。
在这种情况下,您必须启用永久链接
https://yoursite.com/wp-json
)https://yoursite.com/wp-admin/options-permalink.php
).htaccess
请看下面的帮助截图:
答案 14 :(得分:0)
例如,如果您的网站是 https://example.wordpress.com(在 wordpress 上),请使用以下链接,无论任何 API 设置/固定链接等如何,都会为您提供 JSON 响应。
如果您想查看所有可用的端点,请使用此 - https://developer.wordpress.com/docs/api/console/
记得将 **$site**
替换为您的域
在此处查找最新文档 - https://developer.wordpress.com/docs/api/