0

While I was configuring a Ubuntu 14.04 LAMP server yesterday I ran into a rather mysterious problem. After copying the site's files and MySQL database, verifying proper apache2 configuration (apache2.conf and .htaccess) and file permissions, I found that hyperlinks to any page or post returned error 404s.

The homepage loaded perfectly, the Admin Dashboard loaded perfectly, but the pages and posts returned error 404.

However, when the default permalink structure is used instead of post title, pages and posts don't return error 404s.

What is wrong?

  • Just to make sure, you have resaved (flushed) permalinks right? Just go to the settings page, and click save, don't change anything. – Andrew Bartel Oct 8 '14 at 0:40
  • I did make sure to do that. The second part of my answer below is how I fixed it. Turns out mod_rewrite wasn't enabled. – user35544 Oct 8 '14 at 0:50
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy