Posts Give 404 and Page 2 Not Found in WordPress

In and earlier article I discussed a problem I was having with WordPress where links for all posts and pages would result in 404 errors. My solution there worked but not why I thought it had worked and, unbeknownst to me at the time, it didn’t actually fix all the problems. In this article I discuss the correct solution to the problem.

In the previous article I claimed that the correct solution to the 404 problem was to include /index.php/ at the start of the Custom Structure this worked but not for the reason I stated. I was under the impression that Apache was rewriting the URL but needed to match the leading /index.php/ the problem was actually that mod_rewrite was disabled and I hadn’t noticed. Yes, I feel a bit stupid for not checking.

The reason adding /index.php/ to the start of the custom structure worked was because it switched WordPress over to using the PATHINFO method of generating and consuming permalinks. This allows for almost the same pretty permalinks as mod_rewrite just with the addition of /index.php/ at the start of all the URLs. The problem I had was that once I’d added a few pages I noticed that the pagination buttons on the post roll weren’t working. The URL’s being generated looked like this: http://www.example.com/page/2/ and the lack of /index.php/ meant that they weren’t being resolved correctly.

When I wrote the original article I got the “I’ve not understood something” feeling so rather than try to figure out how to add /index.php/ to the pagination link I decided to properly understand the problem. I thought from the start it was strange fix because the quick permalink offerings on the settings page didn’t work out of the box, it turns out they assume mod_rewrite is available which is true almost everywhere.

To make pretty permalinks work correctly the first thing you need to do is make sure mod_rewrite is enabled. There are few ways this can be done but often it’s a case of symlinking the mod_rewrite settings file into the mods-enabled directory either manually us using something like cPanel or Webmin. I would imagine that the vast majority of hosting companies will have already done this for you.

Next you need to make sure that you have Options FollowSymLinks in the directory settings for the virtual host that your site is living in, you will probably also need AllowOverride All. Something like this should work:

DocumentRoot /var/www/www.example.com
ServerName www.example.com
<Directory /var/www/www.example.com>
    Options None
    Options FollowSymLinks
    AllowOverride All
</Directory>

Once you’ve made the changes you’ll need to restart Apache to make it read the settings and then you should find that permalinks work correctly. If you have /index.php/ at the start of your Custom Structure you’ll need to remove it. You can now have permalinks with structures like: /%category%/%postname%/.

See Also

Posted in WordPress and tagged , , .

Leave a Reply

Your email address will not be published. Required fields are marked *