What to do when WordPress posts on a new server return an error 404

Judging from other threads, here are the two most common causes of this problem on freshly built servers:

  • .htaccess override is disabled by apache2.conf
  • Apache mod_rewrite module isn’t enabled.

In the file /etc/apache2/apache2.conf:

<Directory /var/www/>
   AllowOverride All
</Directory>

“AllowOverride” is the switch that tells Apache whether to listen to .htaccess files or not. WordPress relies on the .htaccess file in its’ top directory to define URL rewriting, so if AllowOverride is set to “None”, the .htaccess file isn’t honored.

Using your favorite text editor, change “None” to “All” if it isn’t “All” already. .htaccess takes effect immediately, so check to see if WordPress is loading pages and posts properly again.

If it isn’t, let’s look at the contents of that .htaccess file for a moment. (Example below)

# 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

Notice something?

mod_rewrite is an Apache module, or add-on, that actually does the URL re-writing. mod_rewrite is not enabled by default on current Apache installs, but it can be enabled using the following shell commands: (You have to restart the Apache service for changes to take effect.)

$ sudo a2enmod rewrite
$ service apache2 restart

Check to see if WordPress is loading pages and posts properly. Changes are, it should.

Fun fact: The reason the default permalink structure works without mod_rewrite working properly is because the page or post is referenced by the GET parameter “p” and that page or post’s ID number. (example.com/?p=101) GET doesn’t have anything to do with mod_rewrite.