Transitioning from a case-insensitive to a case-sensitive server

Converting sites that have been hosted on a Windows server is often frustrating, as IIS allows files to be accessed with any case in their filename. Here’s a simple solution for a site made of static files, using PHP and an Apache 404 handler:

In 404-case-insensitive.php


/* Copyright 2010 The Internet Company LLC
 * May be copied under the terms of the MIT software license.

    $directory = dirname($_SERVER['REQUEST_URI']);
    $base = basename($_SERVER['REQUEST_URI']);
    if($directory == '/') $directory = '';

    $potential = glob($_SERVER['DOCUMENT_ROOT'].$directory."/*");

    foreach($potential as $e) {
        $e = basename($e);
        if(strtolower($e) == strtolower($base)) {
            header("Location: $directory/$e");

    Header("HTTP/1.1 404 File Not Found");
    echo("Page not found.");


And in .htaccess

ErrorDocument 404 /404-case-insensitive.php