96

I have been using the Wordpress REST plugin WP-API for months now while developing locally with XAMPP. I recently migrated my site to an EC2 instance and everything is working fine except I now get a 404 with the following message whenever I try to access any endpoint on the API:

The requested URL /wordpress/wp-json/ was not found on this server

Pretty permalinks are enabled with the following structure http://.../wordpress/sample-post/ which works fine when navigating to a specific post in the browser.

Here are some details about my setup:

  • Wordpress 4.4.1
    • Not a Multisite
  • WP REST API plugin 2.0-beta9
  • Apache 2.2.22
  • Ubuntu 12.04.5

Any help would be greatly appreciated as I have gone through SO and the WP Support forums for several hours and am out of ideas. Thank you!

Paulo Boaventura
  • 1,365
  • 1
  • 9
  • 29
dsal1951
  • 1,630
  • 1
  • 16
  • 20
  • 1
    After updated permalinks ,try to disable **wp-api** plugin and activate it again, check after that. – Milap Jan 08 '16 at 07:11
  • Also have a look at : https://github.com/WP-API/WP-API/issues/1509 – Milap Jan 08 '16 at 07:12
  • have you updated your wordpress recently? because since 4.4 they made a lot of changes to the REST API – Christophvh Jan 08 '16 at 08:27
  • Hey guys, thanks for the input but neither of these solved my problem. @Milap Already tried deactivating (and uninstalling) after enabling permalinks but this didn't work. The GitHub issue seems to indicate that the problem was taken care of in beta7 but I am using beta9. – dsal1951 Jan 09 '16 at 04:59
  • @Christophvh I am using using WordPress 4.4.1 which is the latest release – dsal1951 Jan 09 '16 at 05:00
  • This is still an issue in 2019 and none of the solutions worked. My rewrite rules WORK and my permalink structure is "Post name". – Daniel W. Oct 24 '19 at 13:03
  • @DanielW. Indeed this still can be an issue, for me it was due to the location rules in NGINX. Solution proved to be the following: https://stackoverflow.com/questions/54794782/woocommerce-rest-api-nginx-error-404-not-found More reading here: 6LccdMIUAAAAAMuwxYW_klA5oFg8OdxwSnu1u2VX – moojen Nov 13 '19 at 09:19
  • @Erri I was not using nginx on the specific site and the reason was a buggy release of WPML. The link might be helpful tho for others, webserver configuration is mostly the source of the issue in this context. – Daniel W. Nov 14 '19 at 11:36

22 Answers22

188

UPDATED NEW WAY

I also faced similar problem in a local project. I used index.php after my project url and it worked.

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

If it displays a 404 error then update permalinks first (see "Paged Navigation Doesn't Work" section

If it works, maybe you need to enable mod_rewrite, on ubuntu:

a2enmod rewrite
sudo service apache2 restart

Installation

The REST API is included in WordPress 4.7! Plugins are no longer required, just install the latest version of WordPress and you're ready to go.

If you're before 4.7:

  1. Download plugin from here: http://v2.wp-api.org/

  2. install and activate it.

Usage

To get all posts:

www.mysite.com/wp-json/wp/v2/posts

For the search functionality, searching for test post looks like this:

/wp-json/wp/v2/posts?filter[s]=test
GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
Jitendra Popat
  • 2,594
  • 1
  • 15
  • 15
  • 1
    The index.php helped me to solve the problem, in my case, I was only missing the mod_rewrite in apache. I updated this answer to include this solution too. – GabLeRoux Mar 07 '17 at 19:29
  • In my case mode_rewrite was previously installed and activated in 16.04 LTS Ubuntu. Then also 404 occurred, then i write a .htaccess file in wp root directory just like that, and it was start working with out writing index.php on API url. # BEGIN WordPress RewriteEngine On RewriteBase /wp/wp-rest-api/ RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /wp/wp-rest-api/index.php [L] # END WordPress – Kaushik Das Sep 19 '17 at 05:26
  • This just helped me fix a pretty frustrating Gutenberg problem. Thanks! – Kelderic Nov 04 '19 at 19:29
  • 1
    Indead of hardcoding the url, use the get_rest_url() function from wordpress. It works no matter how your URLs are configured. – Anax Feb 25 '20 at 14:40
  • To solve the `/index.php/wp-json` issue set `AllowOverride All` for the `Directory` WordPress is in. – tomalec Jan 07 '21 at 19:44
54

I had this problem with the latest WordPress 4.7+. In my case the REST API only worked after I changed the permalinks setting to something other than "Plain", which was the default setting for my installation.

dlaub3
  • 1,107
  • 9
  • 20
50

On WPEngine and WP 4.9.2 I only had to update permalinks to get fresh, newly installed site to return v2 API calls. What I did:

  1. Create site
  2. Browse to http://yoursitename.wpengine.com/wp-json/wp/v2/posts
    • get 404
  3. Go to admin, settings, permalinks, choose "Post Name"
  4. Click "Save Changes"
  5. Browse to http://yoursitename.wpengine.com/wp-json/wp/v2/posts
    • success. page displays JSON response
Andy Roberts
  • 905
  • 8
  • 6
21

It turned out to be a problem with the Apache configuration.

First, I deleted the .htaccess file in the root wordpress directory.

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

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

That did the trick!

dsal1951
  • 1,630
  • 1
  • 16
  • 20
  • In my case mode_rewrite was previously installed and activated in 16.04 LTS Ubuntu. Then also 404 occurred, then i write a .htaccess file in wp root directory just like that, and it was start working with out writing index.php on API url. # BEGIN WordPress RewriteEngine On RewriteBase /wp/wp-rest-api/ RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /wp/wp-rest-api/index.php [L] # END WordPress – Kaushik Das Sep 19 '17 at 05:29
  • this fixed my issue also. it appears the htaccess file contains the redirection and mod_rewrite was enabled but the site was not reading the htaccess file. – Christian Noel Oct 18 '19 at 02:09
  • This solved my problem when direct links were enabled but giving 404. – pbies Jan 10 '22 at 10:39
13

It is the file permission error, apply the following solution:

Edit this file /etc/apache2/apache2.conf Change /var/www/ Permissions from "None" to "All" Restart the apache2 server.

faryal rida
  • 379
  • 3
  • 6
  • 1
    For me, I specifically changed AllowOverride from None to All. – Halfstop Jul 21 '20 at 02:06
  • Change which permission? AllowOverride? When I set it to All, I got the "Not Found" error even on the home page. Full https navigation is possible only when I set Permalinks to "Plain", which isn't a solution. – Mark Lee Aug 15 '21 at 14:39
13

First you've to Check if the WordPress REST API is enabled or not

The best way to check is to visit this URL: https://yoursite.com/wp-json. If you see some JSON response, REST API is enabled. If it’s showing some error page or returns to home page, REST API is not enabled. Then we've to enable it first.

In this case, you've to Enable Permalinks

  1. Visit your page's admin page (Visit https://yoursite.com/wp-json)
  2. settings > Permalinks (visit https://yoursite.com/wp-admin/options-permalink.php)
  3. Make sure Plain is not selected
  4. Choose Post Name (it's a best one)
  5. Then click on Save Changes. That will rewrite/repair your .htaccess

Please see the helping screenshots below:

Permalinks

choosing Post Name

Source

Anand Raja
  • 2,676
  • 1
  • 30
  • 35
  • So i'm integrating Dacast into a site, my wp-json endpoint exists but im getting a 404 GET error on wp-json/dacast/init . When i go to localhost:8888/wordpress/wp-json/dacast/init that also exists. Any ideas on this one? – Sambuxc Dec 01 '21 at 14:19
6

I solved this issue through following steps:

  1. Navigate to ..\Apache24\conf\httpd.conf and search for LoadModule rewrite_module modules/mod_rewrite.so.

  2. Enable rewrite module by removing the # mark.

  3. Replace all the cases of AllowOverride None to AllowOverride All.

  4. Don't forget to restart apache server. :)

Patriotic
  • 2,103
  • 4
  • 26
  • 36
5

Adding "AllowOverride All" (as hinted by other authors before) to my apache virtual host configuration on my Ubuntu server via SSH did the trick for me:

sudo vi /etc/apache2/sites-available/my-website-name.com.conf

and also (if you use letsencrypt):

sudo vi /etc/apache2/sites-available/my-website-name.com-le-ssl.conf

The files should then look like:

<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>

Don't forget to disable and re-enable the site and reload apache to apply the new configuration:

sudo a2dissite my-website-name.com.conf
sudo a2dissite my-website-name.com-le-ssl.conf
sudo a2ensite my-website-name.com.conf
sudo a2ensite my-website-name.com-le-ssl.conf
sudo service apache2 reload
Matthias Luh
  • 503
  • 6
  • 11
4

I had to manually make a .htaccess, set it to chmod 664, and copy the permalink rules into it.

I also played around with

  • Settings > Permalinks
  • Manually updating .htaccess via the code at the bottom of the permalinks page after clicked "Save"
  • Adding "index.php" as one of the other answers suggests
  • Making sure mod rewrite was enabled via a2enmod
Jacksonkr
  • 31,583
  • 39
  • 180
  • 284
  • I had migrated my site using all-in-one-migration plug-in. And there was no .htaccess file to speak of. It was just missing. I just copied the .htaccess from a new wp install and viola, everything worked. – Joe C Sep 01 '20 at 15:56
4

I found that mysite/wp-json/ was not working, but mysite/?rest_route=/ was normal. This was breaking some, but not all, the REST API features used on my site.

The answer to this turned out to be a recent change to how I was running my server. This had broken REST API but this was not apparent until later.

I had changed this domain from using Apache to using nginx, and I had not correctly transferred the .htaccess customisations. The quick solution to this problem was therefore to change back to using Apache. This restored the site to working order immediately.

I will be changing this domain back to nginx in the future but when I do, I will test it and be careful not to affect the REST API.

Pinkeye
  • 61
  • 4
2

For me on new website I didn't had Nginx pretty links setting enabled I add this tolocation / block

        try_files $uri $uri/ /index.php?$args; 
Salem
  • 654
  • 7
  • 24
  • 1
    Thank you sir for saving me hours of work! This is especially valid for Azure App Services using PHP 8.2, as they run nginx instead of Apache. – DSpirit Jul 07 '23 at 14:45
2

If you are on Windows OS change the Permalink Settings as follows:

Custom structure: /index.php/%postname%/

Permalink Settings

Nitin Sawant
  • 7,278
  • 9
  • 52
  • 98
2

On WordPress 6

You can use this URL and no need for URL Rewrite and no need to change your Permalink settings (I saw Gutenberg editor is using this URL to access REST)

http://example.com/index.php?rest_route=/wp/v2/posts
Positivity
  • 5,406
  • 6
  • 41
  • 61
1

I had moved the WordPress install from a subdirectory to another, so in my case the problem was due to the WordPress config in the .htaccess files. It was trying to redirect every page but the homepage to the old directory. It was just a matter of updating olddir to newdir... This tripped me up more than once so I thought I'd put it here...

# 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
mrtnmgs
  • 1,402
  • 14
  • 26
1

I faced the same problem when I migrated my site from cPanel to Google Cloud Compute Engine Instance; problem was of file permissions which was initially caused due to difference in PHP versions of current deployment from previous deployment.

Here is the fix How to deal with GCP WordPress error "This page isn’t working example.com is currently unable to handle this request. HTTP ERROR 500

Zulkifl Agha
  • 175
  • 1
  • 12
1

I was facing same issue on localhost and I solved this issue with just set RewriteBase Path in .htaccess file which is available at root folder of WordPress project setup.

**Example:** 
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /[folder-name]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /[folder-name]/index.php [L]
</IfModule>
1

If you website is https://example.wordpress.com (on wordpress) for example, use the below link which will give you the JSON response irrespective of any API settings/Permalinks etc..

If you want to see all the available endpoints use this - https://developer.wordpress.com/docs/api/console/

Remember to replace **$site** with your domain

Find the latest documentation here - https://developer.wordpress.com/docs/api/

1

I read through a whole bunch of articles and eventually discovered there was a force site to be https plugin installed and activated on my local, however there is no certificate set up. It was making the requests for https when I should have had the https plugin deactivated.

Once deactivated, I was able to make connections to REST API.

Not a very in depth answer but it is was my answer. Hope this saves someone some time!

Jason Is My Name
  • 929
  • 2
  • 14
  • 38
0

If you have tried the other solutions on this page and they haven't worked, I had success with checking for additional .htaccess files in the root folder above your public_html folder (or wherever you have installed WordPress).

I found an extra one which may have come from a previous installation or been accidentally moved there - it was giving conflicting instructions to the 'real' .htaccess file. Deleting it fixed the problem for me.

stringy
  • 1,323
  • 7
  • 16
0

Try to remove any

order deny,allow deny from all allow from ...

from you .htaccess.

0

I had the AllowOverride All option in the my_site.conf file but it was missing on the my_site-le-ssl.conf (for SSL).

After adding the option in this file too, the REST API worked

Pablo Guerrero
  • 936
  • 1
  • 12
  • 22
0

Here's how I fixed it in 2023, had to combine various answers and documentation.

I assume you've installed php, mysql, apache, libapache2-mod-php8.1, run a2enmod php8.1; a2enmod rewrite; to install those modules, and restarted apache.

Now:

# append rewriting to your root htaccess file
echo "RewriteEngine on" >> /var/www/html/.htaccess; 

# allow only reading of this file
chmod 644 /var/www/html/.htaccess; 

# add the <directory> entry for rewrites to your default config under the <VirtualHost> entry
sed -i '/<VirtualHost \*:80>/r /dev/stdin' /etc/apache2/sites-enabled/000-default.conf <<EOF
<Directory /var/www/html>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>
EOF;

service apache2 restart;

Finally you have to manually browse to go to http://yoursite/wp-admin/options-permalink.php and change permalink type to post. The default on installation is Custom and that breaks.

Warning: I'm an Apache newb and have no idea if anything I just did is secure. I'm just using it for local testing.

Richard
  • 14,798
  • 21
  • 70
  • 103