WordPress: Permalinks

July 21st, 2008 · No Comments

Warning: Technical Article

While some things like our Theme can be a work in progress, Permalinks are one of the configuration options that strike me as an immediate necessity. If you’re not aware, permalinks link to your specific blog posts that will always point to that same entry. So if for example you want to mail the url of one of your posts to a friend, you would use the permalink. For more detail you can go to the WordPress Codex.

So by default WordPress uses a reasonable but kind of ugly (by their own admission) url format for your permalinks. The url will look something like: http://www.protoscopic.com/?p=123. Why should this bother you? I have no idea, I don’t even know why it bothered me. However for some reason I felt that a link using the ‘slug’ was more appealing. So I went through the process of changing mine. Its simple but not quite as simple as advertised so I thought I’d share the step I used in case you find them useful.

As the article states you can go to Settings->Permalinks and pick your option of what you want the links to look like. If you’re anal retentive like me you might be somewhat stressed by this consideration. Whatever I pick, I’m stuck with forever. Well I’m sure I could change, but by the time I make that decision millions of rabid readers will be linking to me from all over cyberspace and I don’t want to annoy them.

So one thing I was sure of off the bat was that I wanted to use the ‘slug’ as the main content of the permalink. The only rationale I can justify for my neurotic desire to pretty up my permalink is that I want someone to be able to tell what they’re going to based on the url. Thus the ‘slug’ is a must. So my immediate inclination is to just use the slug and nothing else so it would look like http://www.protoscopic.com/article-slug. That looks fine to me, but the article warns that this will basically screw everything up.

The system uses mod_rewrite to look at the url and change it into the original ugly request internally. So if you have too broad of a rewrite rule then its going to try to turn every request into a ugly post request, including things like style sheets. I also have some javascript and images that are part of static pages that I need to be available so the “%postname%” only option is out. I then tried %post_id%/%postname% to get the numeric element they advocate, but the whole point of that exercise was the ugliness of the number.

I was just about to settle on some nonsense involving the date when I decided to throw caution to the wind and do what made sense to me (always a dangerous proposition). So based on my vague understanding of their mod_rewrite situation I figured as long as my url definition had something at the front to eliminate non-article directories it would be safe. So for now I’ve settled on “articles/%postname%”. My main concern was it would break things like the Passive Stream Calculator. But everything still seems to be working so I’ll roll with it.

As usual the WordPress documentation is good, but there are a few things that could use a slight expanding upon if you’re not overly familiar with Apache. For the mod_rewrite stuff to work you have to set it up in the .htaccess file. This file will be located in the same directory as your index.php, the root of your wordpress installation. Depending on your system administrative settings, the WordPress scripts may not be able to write that file, in which case you’ll have to copy and paste what it tells you to into that file. So your .htaccess file should look something like this:

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

I don’t think this file changes when you change your rule, so once you’ve written it you can probably live without WordPress being able to update it. I don’t really see any reason to be overly permissive so I went ahead and left it read-only. So now you’ve set up your configuration for your pretty permalinks, of course if your system is set up like mine then it still won’t work.

The slickness of the WordPress installation is such that my httpd.conf section for this site looked something like this after install:

ServerName protoscopic.com
ServerAlias *.protoscopic.*
ServerAdmin [email protected]
DocumentRoot /var/www/wordpress/

I’m always impressed at how easily these php sites work, I barely had to do anything to get most of the WordPress features running. However to allow the mod_rewrite rules to work I needed to change it as follows:

ServerName protoscopic.com
ServerAlias *.protoscopic.*
ServerAdmin [email protected]
DocumentRoot /var/www/wordpress/
<Directory />
Options FollowSymLinks
AllowOverride FileInfo

The AllowOverride tag defines what the .htaccess file is allowed to override. In this case all we need is FileInfo so that’s all I added. I don’t see any particular harm in adding All, but when I doubt I tend to be less permissive. The FollowSymLinks Option tells Apache its okay to follow symbolic links in the file system and is required by mod_rewrite for security reasons as well. For more detailed information see the mod_rewrite documentation and Apache Directives documentation.

Once I’d done all of that my permalinks worked like a charm…now lets just hope I’m still happy with my url decision next week.

Categories: Applied Use

Related Posts:

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

Leave a Comment