Permalinks in WordPress
I’m sure everyone new to WordPress looked through the administration panel, and stumbled upon the Permalinks Settings.
So what are they?
In short: to make the links to your posts/pages/categories/tags look nicer than they do by default.
The default for WordPress is to not use permalinks at all. This gives a quite ugly URL for a post like this:
http://www.yourdomain.tld/?p=20
, which also isn’t very SEO-friendly (SEO = Search Engine Optimization). Categories are shown by default with an URL like this:
http://www.yourdomain.tld/?cat=5
, which isn’t helpful either. Tags look a bit nicer, but not as nice as one would wish for:
http://www.yourdomain.tld/?tag=sometag
.
Permalinks should never change once configured (perma is short for permanent), since other people and search engines will use these to link articles from their site to yours. So you might want to think a bit about how you would like to structure them.
Using permalinks require actually two things: the settings in WordPress and the rewrite engine of Apache, which has to translate the much prettier, SEO- and user-friendly URLs into the internal
'/?p=somenumber'
structure. I only describe Apache here, since that’s the web server I use, how to configure this in other web servers like Microsoft IIS is out of the scope of this article.
The Permalink Settings in the administrator panel gives you five different options for permalinks:
The first option is to use the default, which is the easiest to configure, since you don’t need to configure (or even have) the rewrite module of Apache, but gives you the ugly URLs.
The second one is titled ‘Day and name’, which will order your posts in a /year/month/day/title-of-your-post/ structure. I suggest this one to use when you have more than one posts a day.
A small warning might be given here: although it looks like your posts are going to be scattered around your site, they aren’t: all of your content is still stored in the MySQL database and nothing is going to be created on your site, except a small file called .htaccess (I will come to that later). Also the posts/pages/tags/categories aren’t restructured in any way, internally they are still called p=20, cat=5, and so on. With permalinks you’re simply telling Apache to rename the URL your reader requests in something WordPress can understand.
The next option in the list is called ‘Month and name’, which is basically the same as the second, but without the date. This one could be used if you have more than one post a month, but not more than one per day.
‘Numeric’ displays your posts as a number, but without the question mark. I do not suggest using these, since they’re not SEO friendly (in fact, I think they’re as ugly as the default ones).
The last one is the one I like: ‘Custom Structure’. Since I don’t post that much, I chose this one to create URLs like on my site:
http://www.yourdomain.tld/the-title-of-my-article/
.
WordPress asks you to create your own structure and a number of variables can be used for this:
- %year%: the year of the post (2008, 2009)
- %monthnum%: the month of the post in digits (01, 02, 03, and so on)
- %day%: the day of the post (09, 10, 11, …)
- %hour%: hour of the post, 24-hours
- %minute%: the minute part of the time of the post
- %second%: for the real freaks
- %postname%: the title of the post (actually called the slug). Slugs are always lowercase.
- %post_id%: the post ID, which is the unique idetifier of a post. This is the number you would see when using the default
- %category%: the category of the post. If a post is contained in more than one category, then the category with the lowest number is shown here
- %tag%: the tag with the lowest ID contained in a post
- %author%: the name of the author
According to the codex you really shouldn’t start permalinks with either %category% or %tag% because of performance reasons.
I use only /%postname%/ on my site.
If you make a change to the Permalink Settings, WordPress responds with ‘You should update your .htaccess now‘ and shows a suggestion ow how your .htaccess should look like. If the user account your web server runs on has write access to the root directory of WordPress, then WordPress will automatically create a .htaccess file if it does not exist, or modify if it exists.
In my case, my .htaccess file looks like this:
<ifmodule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</ifmodule>This .htaccess file needs some attention.
First of all it will be in the root of your WordPress directory structure (where your index.php is). If WordPress wasn’t able to create or modify the file for you, you’ll need to create one yourself, or modify the existing one. Depending on your setup that would mean either downloading the file from your site to your desktop with FTP, modify and upload again, or, if you’re lucky like me and have shell access, cd to the correct directory and use your favorite editor (like vi) and edit the file on the server.
This however is no guarantee that the permalinks actually will work. In order to make them work, your Apache might need some modifications also.
First of all, the module mod_rewrite needs to be loaded in your Apache configuration. For this to happen the following line needs to exist in your httpd.conf:
LoadModule rewrite_module libexec/apache22/mod_rewrite.so
The directory of the actual module might vary. This tells Apache to know about rewrite requests it might get. Otherwise Apache doesn’t know how to handle these requests and will give an error. Loading more modules in Apache however means that it’ll perform slower and uses more memory (it isn’t that much slower after loading the rewrite module, but you need to be aware of it: the more modules, the slower and the more memory – this applies to almost every application).
The next thing to reconsider is security. Since a .htaccess file will try to override existing configuration parameters, you need to tell Apache in its central configuration file that it is allowed for .htaccess files to do so.
In my central configuration, for this particular web site this is in it to allow overriding parameters:
<directory "/usr/local/webroot/htdocs">
Options FollowSymLinks
AddOutputFilterByType DEFLATE text/html text/plain text/xml
AllowOverride All
Order allow,deny
Allow from all
</directory>The AllowOverride parameter is the one needed. I chose ‘All‘, but for the mod_rewrite module to work, only ‘FileInfo‘ is needed (according to the Apache documentation). ‘All‘ of course includes ‘FileInfo‘.
This is all there is to tell about permalinks: think about them, configure them, and never touch em again
This entry was posted on Thursday, November 19th, 2009 at 9:36 pm and is filed under webpublishing. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
can you tell me how to make second permalink in wordpress posting?
Permalinks is something that the outside world uses to access your articles directly. While changing the permalink settings in WordPress (which are basically Apache rewrite instructions) you can still access your pages through ‘http://www.yourdomain.tld/?p=20′ for instance.
I don’t think you can configure WordPress to have more than one different permalink setting, you’re free to do whatever you want through your Apache configuration, although I don’t understand what you’re trying to accomplish: permalinks are meant (or at least come in handy) to access your articles in an uniform way.