Keywords: WordPress REST API | 404 error | Apache configuration | permalinks | mod_rewrite
Abstract: This article provides an in-depth analysis of common causes and solutions for 404 errors in the WordPress REST API after migrating from local to server environments. It covers key technical aspects such as Apache configuration, permalink settings, and the mod_rewrite module, offering a complete workflow from basic checks to advanced debugging. Drawing on real-world cases from Q&A data, it explains how to resolve API access issues by enabling mod_rewrite, updating permalinks, and using the index.php prefix, including details on the built-in API in WordPress 4.7+.
Problem Context and Diagnosis
When migrating a WordPress site from a local environment (e.g., XAMPP) to a production server (e.g., an EC2 instance), REST API endpoints may suddenly return 404 errors, even if permalinks function correctly. This inconsistency often stems from server configuration differences rather than core WordPress functionality. The specific error reported is: The requested URL /wordpress/wp-json/ was not found on this server, indicating that the Apache server cannot properly route API requests.
Core Solution Analysis
Based on the best answer from the Q&A data, the key to resolving this issue is ensuring the Apache mod_rewrite module is enabled and configured correctly. On Ubuntu systems, this can be done with the following commands:
a2enmod rewrite
sudo service apache2 restartIf the problem persists after enabling, a temporary workaround is to add an index.php prefix to the API URL, e.g., http://localhost/myproject/index.php/wp-json/wp/v2/posts. This bypasses rewrite rules and processes requests directly through WordPress's entry file, but long-term fixes should address rewrite configuration.
Importance of Permalink Configuration
Multiple answers highlight the impact of permalink settings on REST API functionality. In the WordPress admin dashboard, navigate to "Settings" > "Permalinks" and ensure "Plain" mode is not selected. Updating and saving these settings refreshes rewrite rules, which often resolves API 404 errors. For example, after choosing the "Post Name" structure, API endpoints should respond with JSON data normally.
WordPress Version Compatibility
Note that WordPress 4.7 and later versions include the REST API built-in, eliminating the need for additional plugins. For older versions (e.g., 4.4.1 in the example), install and activate the WP REST API plugin (such as v2.0-beta9). During migration, ensure plugin compatibility and server environment alignment to prevent API failures due to version conflicts.
Systematic Debugging Workflow
It is recommended to troubleshoot using the following steps:
- Check Apache error logs for detailed error messages.
- Verify that the
.htaccessfile exists and contains WordPress rewrite rules. - Test if permalinks work correctly in a browser (e.g., by accessing post pages).
- Attempt to access the API with the
index.phpprefix to isolate rewrite issues. - Ensure file permissions allow Apache to read
.htaccessand WordPress files.