Pragmatism in the real world

WordPress 2.0 Rewrite Rules

We recently upgraded my wife’s blog from WordPress 1.5 to 2.0 and it broke the mod_rewrite rules used to support the legacy Movable Type urls. When she migrated from Movable Type to WordPress, we moved to /{year}/{month}/{day}/{slug} type urls from the default Movable Type rule of /archives/{id}.html. We read the manual at the time and put the following into the .htaccess file:

RewriteRule archives/0*(d+).html /index.php?p=$1
RewriteRule index.rdf /index.php?feed=rdf
RewriteRule index.rss /index.php?feed=rss
RewriteRule index.xml /index.php?feed=rss2

and all was well…

Then we upgraded to WordPress 2.0.

They stopped working because WordPress 2.0 does it’s own mod_rewrite. I worked my way through the source code and modifed the core stuff and got it working. Then I started trying to find out how to do it properly without modifying the WordPress source code. I finally found a post on the support forums from someone trying to do something similar and modifed the solution to work for me:

Add the following to a file called functions.php in your theme directory:

function mt_filter($rules) 
{
    $rules['archives/0*(d+).html'] 
                = 'index.php?p=$matches[1]';
    $rules['index.rdf'] = 'index.php?feed=rdf';
    $rules['index.rss'] = 'index.php?feed=rss';
    $rules['index.xml'] = 'index.php?feed=rss2';
    return $rules;
}
add_filter('rewrite_rules_array', 'mt_filter');

and that’s all there is to it.

Fairly obvious I would think if you know how WordPress works. There’s probably a very handy article somewhere on wordpress.org detailling this, but I couldn’t find it… probably didn’t look hard enough!