WordPress permalinks and Apache VirtualHost

Ok let’s ease back into the blogging with a quickie.

So you’ve done the famous WordPress 5 minute install on your own server, created a new Virtualhost entry for apache.

So far it looks good, and now you’re ready to start tweaking it to your taste. Inevitably one of the first things you’re do is change the posts to use friendly or “permlinks” urls. This allows you to see the post title in the url i.e. http://nicholaspaulsmith.com/my-post rather than an arbitrary post id. In turn this will hopefully give you some benefits with SEO.

Likely however you will now be seeing a 404 error. Now the helpful folks on the internet keep pointing you to this page:


There’s a lot of reading there and a fair bit not very useful, the important part relates the apache rewrite mod configuration.

So given that you’re running a recent version of apache where VirtualHost entries are made in separate files you have probably got apache configuration along the lines of:

1) An entry in your ports.conf :

NameVirtualHost *:80  
Listen 80  
Indicating you are listening for traffic on the standard http port  

2) You will then have one or more sites defined /sites-available in files along the lines of mydomain.com and myblog.com, which relates to your WP install. In you myblog.com entry you probably have minimum configuration along the lines of:

    DocumentRoot /var/www/myblog.com/wordpress     
    ServerName www.myblog.com
    ErrorLog /var/www/myblog.com/logs/error.log 

Now you need to include the url rewrite configuration from the WordPress permalink help.

    DocumentRoot /var/www/myblog.com/wordpress
    ServerName www.myblog.com
    ErrorLog /var/www/myblog.com/logs/error.log
    <Directory "/var/www/myblog.com/wordpress">
         RewriteEngine On
         RewriteBase /
         RewriteRule ^index\.php$ - [L]
         RewriteCond %{REQUEST_FILENAME} !-f
         RewriteCond %{REQUEST_FILENAME} !-d
         RewriteRule . /index.php [L]

If you’re not using the rewrite module anywhere else, it comes with apache, but needs to be enabled.

sudo a2enmod rewrite

Then restart apache and you should be good to go.

WordPress permalinks and Apache VirtualHost
  • Rasca

    Hey you forget the at the bottom

  • proctologsOfZion

    Worked for me. Had those 404’s after installing locally with Apache and VirtualHost. Thanks.