最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rest api - wordpress wp-json prefix issue

programmeradmin1浏览0评论

I'm using Wordpress 4.9+ which I believe enabled with REST api by default. My permalinks are set to Month and Name And I'm using Foxhound React theme for my site.

Now my rest api should be available in

But its available in

.php/wp-json/wp/v2/posts

This is why my theme gets 404 error when tries to get data from api.

Now my question is how do I make my REST available without the /index.php prefix

I've manually deployed everything in aws ubuntu micro with a Lamp server. Wordpress files are in the root folder (/var/www/html/)

Also I have tried mod_rewrite module using the following steps

a2enmod rewrite
sudo service apache2 restart

And this is my .htaccess file

# 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]
</IfModule>
# END WordPress

I'm using Wordpress 4.9+ which I believe enabled with REST api by default. My permalinks are set to Month and Name And I'm using Foxhound React theme for my site.

Now my rest api should be available in

http://example/wp-json/wp/v2/posts

But its available in

http://example/index.php/wp-json/wp/v2/posts

This is why my theme gets 404 error when tries to get data from api.

Now my question is how do I make my REST available without the /index.php prefix

I've manually deployed everything in aws ubuntu micro with a Lamp server. Wordpress files are in the root folder (/var/www/html/)

Also I have tried mod_rewrite module using the following steps

a2enmod rewrite
sudo service apache2 restart

And this is my .htaccess file

# 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]
</IfModule>
# END WordPress
Share Improve this question edited Jul 11, 2019 at 5:02 Philippe Delteil 1034 bronze badges asked Jun 7, 2018 at 7:10 Faysal AhmedFaysal Ahmed 1311 silver badge4 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

You need to set the permalink structure of your website.

  1. Goto Settings > Permalink.
  2. Remove index.php from the Custom Structure.
  3. Click on Save Changes.

It re-generate your permalink structure. And you'll be able to access the posts with http://example/index.php/wp-json/wp/v2/posts

Updated:

.htaccess code like below:

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

# END WordPress

Note: Change dev.fresh with your own domain.

You are may trying to get your url like http://example/...../posts instead of http://example/index.php/...../posts

It's the problem of your server. Your server doesn't support that because you're may using Lightpd server.

You can edit rewrite_once in lightpd.conf if it's lightpd server. Also, if your server support htaccess you can add this line to rewrite url.

"^(.+)/?$" => "/index.php/$1"

This is for lightpd and this regex you can use for htaccess.

Update: As you've said your server support htaccess, use this code in your htaccess

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index.php
RewriteRule ^index.php$ http://example/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
发布评论

评论列表(0)

  1. 暂无评论