Redirect uppercase to lowercase urls in WordPress

I`ve coming across many crawl errors in google webmastertools, which are due to uppercase letters in urls. So I`ve started to look for an easy solution to redirect uppercase to lowercase urls in WordPress. It took me some time to find the action I needed to hook for, so I`m sharing my solution.

How to redirect uppercase to lowercase urls in WordPress by hooking to the parse_request action

The function have to cover three bases:

  1. Get the request before it is executed.
  2. Check if the request contains upper case letters.
  3. And if it does use 301 redirect to forward it to its lowercase equivallent.
For the first base you just need to hook your function to the parse_request action.  If you`re not familiar how to hook functions to actions visit the WordPress Codex, you can start with the add_action documents and keep on reading the links from there. Learn it, love it and use it! This is one of the biggest advantages of WordPress framework and precisely what gives its flexibility.
add_action('parse_request', 'parseUppercase');
function parseUppercase($wp) {
}
For the second base you just need a function which checks for uppercase letters in the string. My choice was using a simple RegEx function which returns boolean
function isPartUppercase($string) {
 return (bool) preg_match('/[A-Z]/', $string);
}
And for the third final base the only good solution to redirect uppercase to lowercase urls in WordPress is wp_redirect. If you`re not familiar with the function you can find useful information about it here.

Installation:

Download the plugin, paste it in your plugins folder and activate it. As simple as that. Try accessing some urls containing uppercase letters after the installation and voila, no more crawl errors due to uppercase letters in urls.

Be the first to comment

Leave a Reply

Your email address will not be published.


*