3

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.com/wp-json/wp/v2/posts

But its available in

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

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

image error

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

2 Answers 2

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.com/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.

2
  • I've updated the question with my htaccess file. And also i'm not using custom structure permalinks. i'm using month and name. I don't see any prefix except for the domain name in the interface Commented Jun 7, 2018 at 8:18
  • First 3 steps worked for me. Commented Jul 11, 2019 at 1:38
1

You are may trying to get your url like http://example.com/...../posts instead of http://example.com/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.com/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
8
  • I have updated the questions with my htaccess file. can you please tell me which line i need to modify. Sorry i'm noob in wordpress Commented Jun 7, 2018 at 8:04
  • @FaysalAhmed Does your server support htaccess?
    – AA Shakil
    Commented Jun 7, 2018 at 8:05
  • yes it supports htaccess Commented Jun 7, 2018 at 8:15
  • @FaysalAhmed I've updated please check out
    – AA Shakil
    Commented Jun 7, 2018 at 8:29
  • did that replacing with my domain. still dont work Commented Jun 7, 2018 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.