How to Resolve WordPress 404 Permalinks and REST API Route Failures
How to Resolve WordPress 404 Permalinks and REST API Route Failures
A frequent issue in WordPress administration occurs when the homepage loads correctly, but every subpage, post, or custom post type returns a 404 Not Found error. Simultaneously, mobile apps, JavaScript frontends, and the WordPress Block Editor (Gutenberg) report:
“The REST API encountered an unexpected result: 404 Not Found (restnoroute)”
This guide explains why URL rewrite failures occur and provides verified fixes across Apache, Nginx, and WordPress core settings.
1. Why Permalink 404s Occur
When you switch WordPress from the default “Plain” URL structure (?p=123) to “Post name” (/sample-post/), WordPress relies on the web server (Apache or Nginx) to route every inbound request through index.php.
If the web server is not configured to rewrite requests, it looks for an actual physical folder named /sample-post/ on the server disk, finds nothing, and returns an HTTP 404 status.
2. Quick Fix: Flushing Rewrite Rules from Admin
In many cases, the rewrite rules cached in the WordPress database table wp_options are simply out of sync after activating a new plugin or registering a custom post type.
- Navigate to Settings > Permalinks in the WordPress admin panel.
- Scroll to the bottom of the page without making any changes.
- Click the blue Save Changes button.
flush_rewrite_rules() internally and attempts to re-generate the .htaccess file automatically.If this fixes the issue, your permissions are correct. If subpages still return a 404, proceed to Step 3.
3. Fixing Apache .htaccess Configuration
If your WordPress root directory is missing an .htaccess file, or if the file is not writable by the web server, you must create or update it manually.
Standard WordPress .htaccess Rules
Create a file named .htaccess in your WordPress root directory (e.g., C:xampphtdocsmy-site.htaccess or /var/www/html/.htaccess):
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
http://localhost/xyphla-support/), adjust RewriteBase and the fallback line accordingly:> RewriteBase /xyphla-support/ > RewriteRule . /xyphla-support/index.php [L] > ``` ### Enabling `mod_rewrite` in Apache If `.htaccess` is present but ignored, Apache's rewrite module may be disabled. #### On XAMPP (Windows): 1. Open `C:xamppapacheconfhttpd.conf` in a text editor. 2. Find the line: ```apache #LoadModule rewrite_module modules/mod_rewrite.so ``` 3. Remove the `#` comment symbol at the beginning of the line: ```apache LoadModule rewrite_module modules/mod_rewrite.so ``` 4. Find the `<Directory "C:/xampp/htdocs">` section and change `AllowOverride None` to: ```apache AllowOverride All ``` 5. Save `httpd.conf` and restart Apache via the XAMPP Control Panel. #### On Ubuntu / Debian Linux:bash
sudo a2enmod rewrite
sudo systemctl restart apache2
---
## 4. Fixing Nginx Server Blocks for WordPress
Nginx does not read `.htaccess` files. URL rewriting must be declared directly in the site's server block configuration:
nginx
server {
listen 80;
servername example.com;
root /var/www/html;
index index.php index.html;
# Core WordPress rewrite rule
location / {
tryfiles $uri $uri/ /index.php?$args;
}
# Pass PHP scripts to FastCGI
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgipass unix:/var/run/php/php8.2-fpm.sock;
fastcgiparam SCRIPTFILENAME $documentroot$fastcgiscriptname;
include fastcgiparams;
}
# Pass Authorization Header for REST API / JWT Authentication
fastcgipassheader Authorization;
}
After modifying the configuration, test and reload Nginx:
bash
sudo nginx -t
sudo systemctl reload nginx
---
## 5. Troubleshooting REST API 404 Failures
If subpages work but the WordPress REST API endpoints (`/wp-json/`) return 404 errors, check the following:
### A. Verify REST API Endpoint Availability
Open your browser and navigate to:
text
https://your-domain.com/wp-json/
You should see a JSON document detailing your site namespaces and routes:
json
{
“name”: “Xyphla Support Hub”,
“description”: “Enterprise Technical Desk”,
“url”: “https://your-domain.com”,
“namespaces”: [
“wp/v2”,
“xyphla/v1”
]
}
### B. Fallback URL Check
If `/wp-json/` fails with a 404, test the non-rewritten endpoint:
text
https://your-domain.com/index.php?restroute=/
If the fallback URL works, this proves that the REST API engine is functioning normally, but your web server's URL rewrite rules are not directing requests to `index.php`. Review the `.htaccess` or Nginx `try_files` configuration above.
---
## 6. Programmatic Rewrite Flushing in Custom Plugins
If you register custom post types or custom taxonomy terms in code, flush rewrites **only upon plugin activation**, never on every page load:
php
/**
Flush rewrite rules safely on plugin activation
/
registeractivationhook(FILE, function() {
// 1. Register custom post type first
xyphlaregistercustomposttypes();
// 2. Flush rules once
flushrewriterules();
});
“
> [!WARNING]
> Calling flushrewriterules() on init or standard hook cycles causes severe performance degradation, as it rewrites the database option and regenerates the .htaccess` file on every HTTP request!
Was this technical article helpful?
Help us continuously refine Xyphla documentation.