PHP Snippet to Lowercase URL and Redirect

Recently I had to ensure that any URL with an uppercase letter was rewritten. Normally I would do something like this using the .htaccess file and mod_rewrite rules. In order for that to work you have to ensure that the RewriteMap directive is defined in the httpd.conf file, then called from .htaccess. Unfortunately this is a shared server so no access to httpd.conf.

I solved it by adding the following to the beginning of index.php.

if (preg_match('/[A-Z]/', $_SERVER['REQUEST_URI'])) {
  $lowercase_file_url = strtolower($_SERVER['REQUEST_URI']);
  header('Location: ' . $lowercase_file_url, true, 301);
  exit();
}

Easy.

Contact Me

Feel free to contact me.

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.