Friday, 21 February 2014

How-to: Fix Permalink issues in Wordpress


Wordpress is the most used blogging platform on the planet. It's easy enough to set-up your own site you understand how to set-up a web server (if not see here and here) and run the installer.

You can load up lots of content very quickly. And then you notice the page addresses are completely useless. So you change them - in Wordpress terms, the permalinks. You want something sensible like the page title at the end of your URL's Then you get:

/%postname%/ 404 not found

No, you're not going crazy, you're not a useless web master. But you may have missed a step. It can be fixed.


First you need a .htaccess file in your site root directory: it could be /var/www/mywebsite or wherever you installed wordpress on. It also needs to be writable, if only by you as the sysadmin; so you need terminal access, a file manager or an FTP program to create the file and set the write permissions.

At the command line you can enter:
touch .htaccess

To set write permissions:
chmod 666 .htaccess
and
chown www-data:www-data .htaccess

Back in your Worpdress dashboard, go to Settings, Permalinks.

Change your permalink to default and save it. This will prove that Wordpress is attempting to write to the .htaccess file and update the page addresses.

If not, you'll get a slightly snippy message, saying that if .htaccess was writable, Wordpress could configure the the rewrite rules in the apache web server, along with code it thinks you need and the suggestion you go do it yourself. Which you can do, if you want. The .htaccess is a plain text file so go paste in the code and off you go.

All being well, you can then change permalinks back to Post Name. Wordpress will update .htaccess with:

    # 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

If you didn't already install it as part of the server stack, you will need to install the appache mod_rewrite (this can also be a reason for a perfectly goood .htaccess not to work).

This is the Apache module mod_rewrite which provides a rule-based rewriting engine to rewrite requested URLs on the fly; or in this case, translate the Wordpress post and page numbers into something more meaningful.

If I recall, apt-get install mod_rewrite does the trick.

You need to enable it from the command line (you need to be root)
    a2enmod rewrite

and then restart apache using
    /etc/init.d/apache2 restart

which should fix the issue, along with a horrbile realisation that you're now more than half way to a place in geek hell. How did that happen? RC

No comments:

Post a Comment

At least try to be nice, it won't kill you...