Local WordPress Install: Posts return 404?

If you have just set up (say) a local Apache2 server and have a local wordpress install so you can debug your website locally you may find (like I did) that while the main webpage comes up fine, when you click through to anywhere else on the site it returns a 404 Not Found error. Even though you know the page/post is there and it shows up OK in the wp-admin section.

For me it turned out that I had not enabled an important plugin on Apache2 to allow permalinks to work. The missing module is called mod_rewrite. It is simple to fix.

Phpinfo()

You can check if you have mod_rewrite active by using phpinfo(). If you don’t have a local file set up to run phpinfo building one is simple.

 

In a text editor write the following:

<?php
    phpinfo();
?>

Save it as phpinfo.php (You could call it doris.php or betty.php if you like but that’s just silly)

Then copy the file into the root directory of your wordpress site. i.e. the same place where the file wp-config.php is.

Now assuming that your local site is accessed by the URL localhost/mysite type the following URL in your browser:

localhost/mysite/phpinfo.php

This will display the php setup on your machine. The top of the output looks like this (versions may be different etc.)

phpinfo top of page

Scroll down to the section marked Configuration. Look in the “Loaded Modules” section. Particularly look for the module “mod_rewrite” If it is not there then in all likelihood that is your problem.

phpinfo configuration section

If it is not there – this is how to enable it.

Assuming you are in a GUI desktop like Gnome or Unity open a terminal with ctrl-alt-t (or however you prefer to open a terminal window)

Enable mod_rewrite

In the terminal type:

sudo a2enmod rewrite

Now we need to restart Apache2. In the terminal type:

sudo service apache2 restart

Fix 000-default.conf

After that we need to edit a file under the Apache2 directory. Assuming your apache2 is installed in directory /etc then edit the following file with the text editor of your choice (gedit if you like a gui, vi if you are a masochist, or whatever).

If it is not under /etc you need to find where your apache2 is installed then under that find the file “000-default.conf”.

Before you start you also need to know where the root of the website area is. Usually with a standard install of apache2 on Ubuntu this is /var/www/html.

So the file I’m editing is : /etc/apache2/sites-available/000-default.conf

Example (assuming you are running a GUI) lets edit it:

sudo gedit /etc/apache2/sites-available/000-default.conf

(If yours is somewhere different then you need to change the path in the command

In the editor add the following in the “<VirtualHost *.80>” block at the end

<Directory /var/www/html>
  Options Indexes FollowSymLinks MultiViews
  AllowOverride All
  Require all granted
</Directory>

So after changing and saving, this file should look like this:

file 000-defaul.conf after having code added to enable mod_rewrite

Restart Apache2 again

sudo service apache2 restart

Check .htaccess is OK

Finally we need to check the .htaccess file is OK. The .htaccess file is in the top directory of your wordpress site. i.e. it is in the same place as wp-config.php and the same place as where you put the phpinfo.php file above.

Usually the directory of your wordpress site files is /var/www/html/mysite

Save the original .htaccess for paranioas sake to htaccess.old

Then open .htaccess in an editor and fix it. As you have saved the original, if you like you can be daring and simply delete the contents and then write this below into it. Change “mysite” (2 occurrences) into whatever your site is called.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /mysite/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /mysite/index.php [L]
</IfModule>

That is about it. Load you site. Do a ctrl F5 on the browser to force a reload and your local wordpress install should be fine. You should now be able to navigate through the site as expected.

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>