<?php
/*
Plugin Name: Old-style permalinks redirect
Version: 0.1
Plugin URI: http://www.fabriziotarizzo.org/sw/sw-en/#wpredir
Description: This plugin translates old-style permalinks in 301-redirects to new-style permalinks 
Author: Fabrizio Tarizzo
Author URI: http://www.fabriziotarizzo.org/
*/
/*  Copyright (C) 2005  Fabrizio Tarizzo

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

function ftr_old_plinks_redirect () {
    global 
$wp_query;
    
    if (
preg_match("/^p\=([0-9]*)$/"$wp_query->query$matches)) {
        
$post_ID $matches[1];
        
$plink get_permalink($post_ID);
        
header("HTTP/1.1 301 Moved Permanently");
        
header("Location: $plink");
        exit(); 
    } elseif (
preg_match("/^m\=([0-9]{4})([0-9]{2})$/"$wp_query->query$matches)) {
        
$year $matches[1];
        
$month $matches[2];
        
$plink get_settings('home') . "/" $year "/" $month "/";
        
header("HTTP/1.1 301 Moved Permanently");
        
header("Location: $plink");
        exit(); 
    } 
}

add_action ('template_redirect''ftr_old_plinks_redirect');
?>