Make URL case insensitive on apache server using PHP

So occasionally it happens…
Someone will leave caps lock on and accidentally enter a url in UPPER case and we all know you will end up at 404 error page, file does not exist..

That is unless you have root access to your server and are able to turn the mod_spelling..
If you have access to root modify the httpd.conf file..

if you do add the following lines and this will make it work:

CheckSpelling On
CheckCaseOnly On

but what about if you do not have ssh access ?
basically this happened on a project I was working on recently (Today to be precise).

Firstly edit your .htaccess file and add these lines..

 ErrorDocument 404 /404.php

basically this declares the error 404 page to be 404.php
Now create a 404.php in the root directory..

Add this code

 

Quick code runthrough…

User goes to yoursite.com/HELLOWORLD.php
the file is helloworld.php

$aurl will be the refering domain in this case : yoursite.com/HELLOWORLD.php
$lurl is then a lowercase version of the url : yoursite.com/helloworld.php

We then redirect to the lowercase page..

But if the page actually does not exist this would send the search into a continual loop.
So we add the next few lines..
if($aurl != $lurl) this line sees if the refering page is the same in uppercase as lowercase..
if it is nt it will redirect to the lowercase url..
If the lowercase url does not exist we end up back on the same page..
This time the referring url will be lowercase and the same…
So we know this time it is a true 404 error and redirect to the 404.html page.

This may not work in all cases as some php setups do not read the ‘REQUEST URI’ properly.
But this solution worked for me and was very easy to setup..

Copyright 2007 - 2024 southcoastweb is a brand of DSM Design.