Apache Case-Insensitivity Howto
From M@Wiki

Main Page | About | Help | FAQ | Special pages | Log in

Categories: Apache | Perl
Printable version | Disclaimers | Privacy policy


Do you need to add case-insensitivity to your website? Here are a pair of solutions that use Apache, mod_perl and Perl. Yes, I know that mod_speling "does" this. As anyone using mod_speling in Apache 2.0 and even moreso in Apache 2.2 knows, it's not as perfect as we'd like, anymore.

NOTE: This solution is known to work on Apache 2.2 using mod_perl 2.0 - It may not work on older versions of either.

Contents

Fix Case

Most of the time, you just want to fix the case when people type it incorrectly into their webbrowser, or someone mis-linked something. The below script, when installed as a PerlTransHandler will do exactly that. If the request exists, the handler passes on and doesn't do anything- But if it doesn't, it recurses through the request URI and case-insensitively builds a URI that does exist.

httpd.conf Changes

In your httpd.conf you'll want:

PerlModule Apache2::RequestUtil
PerlModule fixcase
PerlTransHandler fixcase

Handler Code (Perl)

In /usr/lib/perl5/site_perl/fixcase.pm the below should be:



package fixcase;

use Apache2::Const qw(DECLINED);
use Apache2::RequestUtil ();

sub handler {
	my $r = shift;
	my $file=$r->document_root() . $r->uri();
	unless(-e $file) {
		# File doesn't exist, let's try to find it!
		my @uribits=split(/\//,$r->uri());
		shift @uribits; # shift off the beginning ''
		my $newuri=$r->document_root(); # $newuri is the uri we're building
		my $sofar=0;
		for(@uribits) {
			my $bit=$_;
                        $bit =~ s/\(|\)|\`//g; # stupid url tricks
			$sofar=0; # reset sofar, so we know if we're still on track
			opendir(FD,$newuri) or last;
			for(readdir(FD)) {
				my $dthing=$_;
				if($dthing =~ /^\./) { next; } # safety first;
				if($dthing =~ /^$bit$/i) { # case-insenstive pattern match
					# We have a match!
					$sofar=1;
					$newuri .= "/$dthing";
					last;
				}
			}
			closedir(FD);
			unless($sofar) { last; } # we missed this bit, don't bother recursing further
		}
		if($sofar) {
			# We made it!
			my $dr=$r->document_root();
			$newuri =~ s/^$dr//; # strip off the document_root from the new uri
			$r->uri($newuri); # Set the uri to the new uri
		} # else we can't do anything...
	}
	return DECLINED; # Always pass it on
}

1;


Auto-LowerCase

Maybe you want to make sure your content people lowercase their files... and to make sure they do, you want to automatically lower-case any request that comes in.. so a request for My HomePage.HTML will get turned into my homepage.html...

httpd.conf Changes

In your httpd.conf you'll want:

PerlModule nukecase
PerlTransHandler nukecase

Handler Code (Perl)

In /usr/lib/perl5/site_perl/nukecase.pm the below should be:

package nukecase;

  use Apache2::Const qw(DECLINED);
  sub handler {
      my $r = shift;
      $r->uri(lc($r->uri));
      return DECLINED;
  }

1;

Retrieved from "http://mattwork.potsdam.edu/projects/wiki/index.php/Apache_Case-Insensitivity_Howto"

This page has been accessed 2,611 times. This page was last modified 01:03, 1 November 2006.