By default the standard Permalink structure on WordPress is “Plain” giving you the rather ugly /?p=123 link. If you’ve found that you want to change your permalink structure to post name but find that it keeps giving you the 404 Not Found error when you enable it it’s usually down to the .htaccess file.

So here’s a couple of steps to get your .htacess file working correctly and fixing your pretty /%postname%/ issues:

Connecting to your Apache server

  1. Firstly you’ll need to connect to your Apache server by using your preferred ssh program in my case this would be Terminal on the Mac OS X, with the following command:
    ssh -i /path/to/your/pemfile.pem [email protected]

Configuring the mod_rewrite module

  1. To enable the pretty permalinks you need to have the mod_rewrite module installed on your Apache server, to do this type the following command:
    sudo a2enmod rewrite

Checking for the presence of the .htacess file

  1. Now, you’ll need to change your directory to where your WordPress is configured:
    cd /var/www/html
  2. Now we’ll check for the presence of the .htaccess file:
    ls -al
    •  If you find you that you’re permalinks are not working you’ll usually find that you haven’t got a .htaccess file.
      So we’ll need to run this command to create one:

      sudo touch .htacess
  3. Now we’ll need to edit the .htaccess with the following to make sure WordPress can interpret it and re-write your Permalinks to the new /%postname%/ structure:
    sudo nano .htacess
    # 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]
    # END WordPress
  4. Next, exit the nano editor and save the file, using CTRL + X and then Y to save

Giving .htacess file the correct permission

  1. In order for WordPress to utilise the .htaccess file correct it needs to have the correct permission to read/write to it, to change the permission level use the following command:
    sudo chmod 644 .htaccess

Editing your default.conf

  1. Finally the last step is editing your default.conf file to allow WordPress to make the changes to your links, to do this follow this command to get the editor up:
    sudo nano /etc/apache2/sites-available/000-default.conf
  2. Now you you’ll need to find the line that has, DocumentRoot /var/www/html, and you’ll need to add these lines under it:
    <Directory "/var/www/html">
        AllowOverride All
  3. Next, exit the nano editor and save the file, using CTRL + X and then Y to save

Restarting your Apache Server

  1. Now use the following command to restart your Apache server:
     sudo /etc/init.d/apache2 restart

Change your WordPress Permalink

  1. Now head over to your WordPress Blog, Go to Settings > Permalinks
  2. Change this to Post Name
  3. Hit Save Changes

That should be all, if done correctly your links should now change from the Plain (/?p=123) to the new Post Name structure.

Let me know how you guys get on in the comments below!