Home » Featured, PHP, URL

Redirect Case Sensitive URL Using mod_rewrite and PHP

20 June 2008 No CommentEmail This Post Email This Post

In Wordpress you may have noticed that the page slug defaults to lower case letters. I have learned that it does this in order to prevent potential problems or conflicts between Windows, Linux, and different browers.

Unfortunately, this can also cause problems.

For example, a user recently tried to visit Metro-East.com/bloggers by typing Metro-East.com/Bloggers into the address bar. (This is a simple entry page for bloggers and is not very interesting to look at.) The server treated the URLs as though they pointed to two different pages and so this caused an error because as far as it was concerned Metro-East.com/Bloggers did not exist.

I needed to find a way to make URLs not be case sensitive.

In other words, I had to find a way to redirect from the upper case URL to the lower case URL.

I searched for almost an hour trying to find a tutorial or explanation that I could understand. (My knowledge on these subjects is very limited and I pretty much need things spelled out for me very...very...slowly.)

I finally came across this article at TellinYa.com that explains how to use mod_rewrite on your .htaccess file to call a PHP function that will redirect from lower case to upper case URLs.

In order to implement this fix all you need is access to your server so that you can upload files.

Open your .htaccess file and add these lines:

HTML:
  1. # ReRoute Uppercase Containing URLs
  2. RewriteCond %{REQUEST_URI} [A-Z]
  3. RewriteRule ^(.*)$ lcaseurl\.php\?url=\/$1 [L,NC]

Then create a new file named lcaseurl.php.

Paste this code into lcaseurl.php and put it in your root directory:

PHP:
  1. <?
  2. // Check if URL is passed as parameter
  3. if(strlen($_GET['url'])){
  4.     // Redirect to the lower-case version permanently
  5.     header("HTTP/1.0 301 Resource Moved Permanently");
  6.     header("Location: ".strtolower($_GET['url'])."");
  7.     exit();
  8. }
  9. // If not URL passed return a 404
  10. header("HTTP/1.1 404 Not Found");
  11. ?>

That's it! Problem solved. You can test it out by visiting Metro-East.com/Blogs or Metro-East.com/blogs. They will both go to the same page. (A short list of the different blogs at my website.)

Here is a recent forum post on the subject that is not directly related to the solution I've put forth here. It was one of the most informative threads I found while surfing for answers.

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • NewsVine
  • Propeller
  • Reddit
  • Slashdot
  • SphereIt
  • StumbleUpon
  • Technorati
  • blinkbits
  • BlinkList
  • Furl
  • Ma.gnolia
  • Spurl

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.