Subscribe to gabehabe's on-topic ramblings        RSS Feed

Case Insensitive URLs on a Linux Server

Icon 2 Comments
Only a short entry. This is something that can be a bitch to figure out, and pretty difficult to find the simplest solution. So here I am, providing you with said solution out of the generosity of my heart! :)

So, you may have heard of a file called .htaccess which is part of a configuration for Apache. We'll be using this, plus a one-line PHP page to redirect to the correct file each time. yay!

The problem
We have a Linux server. Linux filenames are case sensitive, and URIs are no different. This can pose a problem, correct?

The solution
This is soooo simple, yet not easy to find. The simplest way would be to redirect any URL which contains uppercase letters to this PHP script, right? (explained later)
So, essentially we will need this:
#enable rewrite engine
RewriteEngine On

#if our URI contains an upper case letter [A-Z]
RewriteCond %{REQUEST_URI} [A-Z]
#rewrite it to our PHP page, passing the given URI as a GET variable
RewriteRule ^(.*)$ [url="{REQUEST_URI}"]{REQUEST_URI}[/url] [L,R=301]

So there we have it! Or not. You might've been an eager beaver and tested it already, before reading on. If you did, you will have noticed that all your images are b0rkT. OH NOEZ! *slaps forehead*

So how can we fix that?!
Simple, actually. We can add another rewrite condition, this one only rewriting any pages which have .php in their extension. And obviously, you could customise it to suit other pages extension, whichever you please! yay!

So, the final htaccess file will look something like this:
RewriteEngine On

RewriteCond %{REQUEST_URI} [A-Z]
RewriteCond %{REQUEST_URI} ^(.+)\.php$
RewriteRule ^(.*)$ [url="{REQUEST_URI}"]{REQUEST_URI}[/url] [L,R=301]

Thanks to Mocker:
If you really wanted, you could also check if the file exists: if it doesn't then we try lowercase. If it does, we go straight to it:
RewriteCond %{REQUEST_URI} !-f

Now, the PHP script is even simpler. We can have it as one line:
<?php (isset($_GET["uri"])) header("Location: " . strtolower($_GET["uri"])); ?>

Or we could expand on it a little, so that if the user browses to the page without being redirected (ie, if $_GET["uri"] isn't set) we can redirect them to our home page:
	if(isset($_GET["uri"]))	header("Location: " . strtolower($_GET["uri"])); 
	else header("Location: ./"); // the index file of this folder. alternatively: header("Location: [url="");"]");[/url]

The drawbacks
Forgot to mention this! Thanks to Mocker for the reminder. You'll have to use name your pages in lowercase in order for this to work, since it we use strtolower()
It may not be the most elegant approach, but it certainly works. It's short and simple, much like my thoughts. :)

Also, it currently only works with URIs that end with .php
The reason for this is that if we start manipulating the case of some of the GET data which comes after the actual filename, we could corrupt the page. It could be modified to only convert everything before the first index of "?" to lowercase, leaving everything after there, but I'm not sure how this would affect the speed of the method. Have a play with it!

So, a very brief blog entry, which will hopefully get you up and running with case insensitive URIs on your Linux box! If you have any questions, feel free to ask: I feel I haven't been as clear today as usual, for which I apologise. :)

Happy coding!

2 Comments On This Entry

Page 1 of 1

snoj Icon

28 January 2009 - 01:20 PM
Great post gabe!

Part of the reason you made this thing was to protect against uri's that had a mix of upper and lower case lettering when on linux the case is sensitive. Why not then build into your redirection php script a check for if the file exists as it is typed?

Though adding checks for actual filenames may add some lag. :/

gabehabe Icon

28 January 2009 - 01:24 PM
You just explained why not! ;)

I'll test it out when I get the time, probably at work tomorrow. I'll add some timers and see just how much of a lag the PHP script would add in checking for the existence of said file. Thanks for the suggestion. :)
Page 1 of 1

February 2017

19 20 2122232425

Request A Topic!

Want me to blog about something? Perhaps a language? A piece of software? A specific topic? Let me know! Even guests can post here on my blog!

If you would like to request a topic, please post a comment here and I'll get on it right away! smile.gif

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)

gabehabe's off-topic ramblings

Follow me on Twitter!
lol, my other blog died a horrible lonely death. Ah well.

Smiley of the [however often I change it]

IPB Image

Contact Me


Google Talk:
Yahoo: gabehabe (rarely used)
AIM: gabehabe (rarely used)

Skype: gabehabe

Want me to work for you? [click]