4

I am getting a 404 on http://localhost/wp-json/wp/v2/posts route. I changed permalink type to plain. Still got the same issue.

If its useful, I am ubuntu 16.04 desktop recent upgrade from 15.10, suffered issues with php5.6 to 7.0 upgrade. Am now on php 7.0

Edit1: Tried every other permalink setting combination, no results!

3
  • As per docs what endpoints are available when you go to http://localhost/wp-json/ ? What version of wordpress? Using plugin or built in version of wp-json? Sure not much detail provided here Commented Jun 25, 2017 at 21:38
  • I am using 4.8, I guess wp-json is built into this version. But I installed REST API v2 plugin(was following some tutorials, may be old ones). Commented Jun 26, 2017 at 5:28
  • FYI I am totally new to wordpress, so ask me for stupid checks too! Commented Jun 26, 2017 at 5:30

5 Answers 5

9

http://localhost/?rest_route=/wp/v2/posts should works. Refer https://developer.wordpress.org/rest-api/using-the-rest-api/discovery/ for details.

Sign up to request clarification or add additional context in comments.

1 Comment

so should the webserver redirect wp-json/* to index.php?rest_route=* I receive the error when trying to save a new post.
4

You need to give permission to your wp-content folder. For the change the permission please use below command in terminal.

sudo chmod -R 755 wp-content/

After the change permission is still show 404 error then allow AllowOverride for apache, below is the step:

navigated to /etc/apache2/sites-enabled and opened 000-default

All of the AllowOverride variables were set to None, which I replaced with All.

After this change enable mod using below command:

 a2enmod rewrite

And restart the apache service below is the command:

sudo service apache2 restart

5 Comments

I don't have AllowOverride variables at all in the /etc/apache2/sites-enabled/000-default
You need to allow read htaccess to apache. for more detail, please click on link: digitalocean.com/community/tutorials/…
After performing above step you need to change permalink from dashboard, please set permalink is post.
I'm terribly sorry to say this, but DON'T DO THIS! This is a really bad and dangerous answer and should not be the accepted one. With sudo and -R you're performing a recursive operation on a whole directory that is not only useless but also potentially dangerous. The wp-contents should not have 777 rights, 755 is sufficient. See wordpress.org/support/article/changing-file-permissions
Thanks For your suggestion. I have modify my post. Thanks
2

First you need to enable the apache rewrite module with the following command:

 sudo a2enmod rewrite

then we must edit the configuration file of our virtual host

sudo nano /etc/apache2/sites-available/your_config.conf

and add in the following directive

<Directory /var/www/your_root_directory>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
</Directory>

note: change your directory path

now our file should look like this

<VirtualHost *:80>
    <Directory /var/www/your_root_directory>

        . . .

    </Directory>
    ServerName midomain.com
    . . .
</VirtualHost>

Now Restart the service of apache

sudo service apache2 restart

create a .htacces file in your root directory of your web site

sudo nano /var/www/your_root_directory/.htaccess

and finally add into .htaccess

RewriteEngine on
RewriteRule ^wp-json/wp/v2/posts$ ?rest_route=/wp/v2/posts [NC]

1 Comment

I wish you explained the "Why" of anything you instructed people to do here.
0

Add index.php after your root link:

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

That worked for me.

2 Comments

Please don't post identical answers to multiple questions. Post one good answer, then vote/flag to close the other questions as duplicates. If the question is not a duplicate, tailor your answers to the question. See Is it acceptable to add a duplicate answer to several questions?
this worked for me in mac running MAMP!
0

This may help who face same problem after choosing pretty links in WP:

https://github.com/WP-API/WP-API/issues/497#issuecomment-420405995

I will put this answer here to stay even original is deleted.

From: ecker00

While this is old, people still seems to find this thread when looking for issues with 404 on wp-json, judging by all the emojis on the previous post. Thought I would share my solution, as my Nginx config was:

location / {
    try_files $uri $uri/ /index.php?$args =404;
}

Removing =404 at the end solves it, but if you don't want wordpress to handle 404 pages it's better to add this in bellow:

location /wp-json/ { # Resolves WP Gutenberg 404 issue
    try_files $uri $uri/ /index.php;
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.