<?php
/*
Plugin Name: Rewrite Rules for Day/Month/Year
Plugin URI: http://frans.lowter.us/2010/01/22/teaching-wordpress-some-manners-enabling-daymonthyear-archives
Description: Wordpress can't cope with /%day%/%monthnum%/%year%/ for some reason. That is to day, it fails when you try to go for an archive in the form of /date/month/year/ This teaches it some manners. Probably/hopefully shouldn't interfere with other structures, but why you'd activate it if you don't need it I wouldn't know.
Version: 1.0
License: GPL
Author: Frans
Author URI: http://frans.lowter.us
*/ 

function test_add_rewrite_rules( $wp_rewrite ) {
	$new_rules = array(
		"date/(\d{2})/(\d{4})" => 'index.php?m=' . $wp_rewrite->preg_index(2) . $wp_rewrite->preg_index(1),
		"date/(\d{4})" => 'index.php?year=' . $wp_rewrite->preg_index(1)
	);
	$wp_rewrite->rules = $new_rules + $wp_rewrite->rules; //NOTE: You must add it to the start of the array, Else WP's greedy rules at the end of the array will eat the request
}

register_activation_hook( __FILE__, 'flush_rules_initiate' );
register_deactivation_hook( __FILE__, 'test_flush_rules' );
// add_action('init','test_flush_rules'); // for testing

function flush_rules_initiate() {
	// Add the permalink override stuff
	add_action('generate_rewrite_rules', 'test_add_rewrite_rules');
	test_flush_rules();
}

function test_flush_rules(){
	//Flush the rewrite rules so that the new rules from this plugin get added,
	//This should only be done when the rewrite rules are changing, Ie. When this plugin is activated(Or Deactivated), For simplicity while developing using WP Rewrite, I flush the rules on every page load
	global $wp_rewrite;
	$wp_rewrite->flush_rules();
}
?>