4

I have a WP installation in my server that uses WP as an API. I use a few plugins to get my data:

My theme is a react app that uses the WP rest API to get info.

This was working fine. I installed WP, the plugins and things went smoothly.

I was doing a few things through FTP on friday and accidentally deleted the .htaccess file's contents (not the file itself). While this file was empty, I updated the permalink's structure. I noticed I broke the blog's frontpage (admin works fine) and restored the htaccess and enabled the permalink setting that I thought was enabled before (fresh install, I think it was the "Month and name" setting that was enabled by default).

For some reason, the API urls now return 404 when permalinks are enabled.

If I use the "Plain" setting on permalinks, things almost work (the frontpage plugin is broken, but that's another story I think).

Anyone has an idea of what might've gone wrong or how I fix it? Here's a link to the live site: http://enrique-ramirez.com/dk/ it'll probably have the Plain setting enabled, though.

This is my current .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

UPDATE So if I try to GET this link: /wp-json/wp/v2/posts it will fail, but if I append index.php it works (so /index.php/wp-json/wp/v2/posts. Notice that I'm not using URL params). I read somewhere that I might need to enable mod_rewrite in my apache? But this was working before... so I'm now even more confused.

enrique-ramirez
  • 914
  • 2
  • 11
  • 23
  • Your .htaccess file looks good, and should work without index.php appended to the API-path. If you save a page on the server with phpinfo(); ?> and visit it in the browser, and then search for mod_rewrite, does it appear as active? Is it hosted on a native Apache? Or emulated? Path to .htaccess might differ depending on this. – ninja Apr 16 '18 at 14:22
  • Hi! Thanks for your reply. I've created a test.php file with phpinfo(); in it, I cannot find mod_rewrite in the file. Maybe something is amiss with my server rather than my WP installation. You can check the file here: http://enrique-ramirez.com/dk/test.php – enrique-ramirez Apr 17 '18 at 05:46
  • Duplicate of https://stackoverflow.com/questions/34670533/wordpress-rest-api-wp-api-404-error – RonanOD Jun 20 '19 at 18:16

2 Answers2

4

I had this exact same issue which points to rewrites not being enabled. However, my server had the rewrite module enabled and the same .htaccess file as above.

The problem was that the /etc/apache2/apache2.conf wasn't allowing .htaccess override files:

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

Changing AllowOverride None to AllowOverride All resolved the issue. Make sure you are targeting the right Directory where your WP site lives!

Then restart your apache process:

sudo service apache2 restart

Note: If you have configured your rest API endpoint to be something other than the default wp-json you might need to go to Dashboard -> Settings and change your permalink settings, then revert it back to what it was. This will rebuild the rewrite cache.

Tom Mertz
  • 626
  • 9
  • 14
2

For Nginx server, open /etc/nginx/sites-available/example.com

Change "location /" part to

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

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}

Then restart your Nginx server:

service nginx restart
Srivaths A
  • 81
  • 1
  • 5