Tue 1 Nov 2011
WordPress Throws 404 Errors on all Pages, Posts and Categories
Posted by Craig under Tech
No Comments
Help, My Permalinks No Longer Work!
I’ve been using the following permalink construct since I first started this blog:
/%category%/%postname%
At some point during a recent upgrade this had the effect of making every single page, post, category page or tag return a 404 Page not Found error.
And not my custom 404 page either but a server 404 error page, even my 404 page was no longer working! Unfortunately I didn’t realise this until I fixed everything, so I missed out on the existential fun of having a 404 page throw a 404 error itself!
/%category%/%postname% Considered Harmful?
According to the WordPress site it is (now?) not considered good practice to use this construct. Hmm… I’ve been using it for years and never had an issue with it so I don’t care about that, and I have one post that is very widely linked and referred to, so if I change the permalink structure I’ll need to put in a permanent redirect just for that post. I just want my blog to work again thanks.
There seem to be an awful lot of people out there reporting this same problem, with various levels of similarity to my issue, but finding an actual solution took a fair bit of effort / trial and error; going through the usual process of troubleshooting – disabling plugins, switching permalinks back to the default, switching to the default theme etc. didn’t throw any light on the problem.
The Mystery Deepens…
I was able to narrow the problem down to the RewriteRules required for the permalink constructs to work. According to the permalink settings page these should be in my .htaccess file in the root level of my wordpress directory.
The odd thing is, there was no .htaccess file there, and neither were there any RewriteRules in my virtual host config file, so I do have to wonder how this ever worked.
Is it possible it was a new “feature” introduced with the upgrade to 3.x that I never noticed? I admit I hadn’t updated for a while and ran the upgrade to 3.something-or-other recently, when I was very busy and not paying attention – who hasn’t just logged in and seen that an update was available, clicked through the Update Automatically process and thought no more about it, right? Alternatively, perhaps the upgrade process somehow removed the .htaccess file?
Anyway, I altered the permissions to allow WordPress to create a new .htaccess file for me, but still had no joy, every link still gave the same 404 error.
The Solution
After much head scratching I eventually figured out that the Apache setup did not recognise the presence of the RewriteRules in the auto-generated .htaccess file. I have no idea why as it wasn’t giving any errors, it was like it couldn’t even see the file. This is odd in that I’ve used various .htaccess files on my sites over the years, including complex RewriteRules to block a widespread content leaching problem, and have never run into this issue before.
The solution for me was simply to add these rules to the virtualhost config file and restart apache, and hey presto all my links suddenly work again thank you very much!
The relevant RewriteRules section to add is as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Obviously set the RewriteBase to the folder you have installed WordPress into if it is not at the root level of your site.
I have no idea what changed or why the .htaccess file was no longer working, but hopefully you might find this information useful if you’re in a similar pickle!

Twitter