Accepting both short and descriptive URLs with Wordpress

I was asked whether it was possible to use more descriptive URLs for my blog posts instead of using the post ID only. Wordpress permalinks only allow to have one structure active, but that doesn’t mean you can’t change the webserver (lighttpd in my case) to do some redirections.

Until an hour ago, my blog accepted only the following kind of url:

http://example.com/post/871

This was, in my opinion, easier to use since shorter. However, it is not very descriptive. So, we would like to have the following URLs:

(1) http://example.com/more-descriptive-urls
(2) http://example.com/post/871

In Wordpress v3.3 (and also earlier version), you go to “Settings > Permalinks” and change the “Common Settings” to a custom structure like this:

Custom Structure: /%postname%/

This makes more descriptive URLs (1) work, but it breaks our existing shorter URLs (2).

Using lighttpd, we have to change the Wordpress rules so we are redirecting URLs with a post ID, to the descriptive URLs.

url.rewrite-once += (
    # more rules..
    "^" + wpdir + "post/(\d+)/?$" => wpdir + "index.php/?p=$1",
    "^" + wpdir + "(.+)/?$" => wpdir + "index.php/$1"
)

The above redirects http://example.com/post/871 to /index.php/?p=871.

Restart lighttpd and we’re done. Now both the longer, more descriptive URLs will be default, and the old URLs using only the post ID, will redirect to the new location.