How to detect requests to old Movable Type-created pages

| No Comments | No TrackBacks
Movable Type used underscores in the past to represent spaces in file names. For example, hello_word.html instead of hello-world.html. If you create an Apache error handler in PHP, you can use the following code sample to redirect from the old style to the new style without any problems:

$str = $_SERVER{'REQUEST_URI'};
$matches = array();
if (preg_match("/\/[\d]+\/[\d]+\/([\w]+_)+[\w]+\.(html|php)/", $str, $matches))
{
    $filename = str_replace("_", "-", $matches[0]);

    if (file_exists("." . $filename))
    {
        header ('HTTP/1.1 301 Moved Permanently');
        header ('Location: '. "http://www.codemonkeyramblings.com" . $filename);
    }
}
That will convert a request for, say, http://www.domain.com/2008/11/hello_world.php into http://www.domain.com/2008/11/hello-world.php. I'm using that now in my HTTP error handler.

Related Entries:

Printer-friendly version

No TrackBacks

TrackBack URL: http://www.codemonkeyramblings.com/mt/mt-tb.cgi/735

Leave a comment

June 2009

Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        

Tips

OpenID accepted here Learn more about OpenID

Creative Commons License
This blog is licensed under a Creative Commons License.
Powered by Movable Type 4.261