我有一个 wordpress:https://example.com 我需要使用 WP REST API。
在 .htaccess 中:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
</IfModule>
# END WordPress
我在我的 wordpress 上使用了 Basic-Auth 插件:https://github.com/WP-API/Basic-Auth
不过我有:
{
"code": "rest_cannot_access",
"message": "Only authenticated users can access the REST API.",
"data": {
"status": 401
}
}
我测试了 http 和 https。
怎么了?
最佳答案
解决了!
我改成了下面的
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
HTTP_AUTHORIZATION 规则必须在其他规则之前,这是因为存在 L 标志,L 标志表示(最后 - 停止处理规则),因此如果它在原始规则之后它永远不会到达该规则WordPress 规则,
关于wordpress - WP REST API 和 "Only authenticated users can access the REST API",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50413414/