1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Any CODE or Script for making URLs case insensitive?

Discussion in 'Site & Server Administration' started by kslokesh, Nov 1, 2008.

  1. #1
    I want to make the URLs case insensitive..

    eg: HappyBirthday.html ==> happybirthday.html

    in HTML, src="abcd.jpg" gives error if the file name of the image is abcd.JPG .

    is there any method to make them case insensitive?

    I use only lowercase in file names and directories.. so I want to make their access case insensitive..
     
    kslokesh, Nov 1, 2008 IP
  2. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Are you using apache?
    If so, do you have access to it's configuration file (httpd.conf)? (probably not on shared hosting)
     
    keyaa, Nov 2, 2008 IP
  3. kslokesh

    kslokesh Well-Known Member

    Messages:
    153
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #3
    Thanks Keyaa.. But how can it be done? what code should I put..

    And ya, I use shared hosting.. So is there any other method like editing .htaccess or any PHP scripting for doing this work?

    Please tell me what to edit in httpd.conf file as I use Apache on local system to test files locally..
    I want to try it on my local system also.
     
    kslokesh, Nov 3, 2008 IP
  4. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Can't think of a really good way to do this on shared hosting.

    Like most things it's still possible though. The best solution I could come up with is this:

    Set a php file as an ErrorDocument for Error 404 (File not found):
    this goes into .htaccess:
    ErrorDocument 404 /urltolower.php
    Code (markup):
    This is your urltolower.php:
    <?php
      $request = $_SERVER['REQUEST_URI'];
      $lower = strtolower($request);
      if($request == $lower) {
        // already lowercase, and file was not found
        include_once('error_404.html'); // your 'file not found'-page
      } else {
        // non-lowercase file not found - redirect to lowercase file
        header('HTTP/1.1 301 Moved Permanently');
        header('Location: ' . $lower);
      }
      exit();
    PHP:
    then you may create error_404.html which will be shown if a file really doesn't exist at all. And that's it (if I didn't miss something ;)). All your files should be lowercase like you stated though! This should work for most shared hosting configurations. With some, it "works", but they still spit out a "404 file not found" header while redirecting to the correct file - this is bad since search engines don't "see" your files this way.
    Give it a try, once it's on the server you can check the header that's being returned by your server with the "Live HTTP Headers" Firefox extension, for example, or I'll do it if you post back.
     
    keyaa, Nov 3, 2008 IP
  5. kslokesh

    kslokesh Well-Known Member

    Messages:
    153
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #5
    Thanks a Lot Keyaa... Really great thing.. Actually I dont know PHP language.. But I try to understand it.. I added the "?>" as the last line in to the PHP code you had provided.

    It really works with the URLs...

    Check it here: http://lokeshks.co.cc/

    I have put an HTML file test.html and check it. It is really working..

    The Access Log Shows the 301 for the uppercase ones.. I think the search engines will follow and reach the correct URLs which are in lowercase..

    I have placed an image called testlog.jpg you check it, the log snapshot of the hosting.

    You had said to check Live HTTP Headers, It gives 200 status ok.. You check it from your side also..

    This code will prevent the 404 errors if someone put our URLs in a different case. The requirement is that we must use a single casetype in out URLs and filenames...

    I think you can modify this code a bit so that it will neglect the part of the URL after the symbols like ? # & (dynamic URLs and Session IDs etc.) and make it work in a more wider areas...

    I suggest you to copyright the script if its written by you and place an Author and Copyright Notice also..
     
    kslokesh, Nov 4, 2008 IP
  6. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Good to see it works, never actually checked it, just wrote the code here ;)

    I'm glad to tell you that the header status codes are all fine!

    FYI, the PHP end tag "?>" is optional, and it's use even prohibited under Zend coding guidelines. But stick to whichever coding style you like. Omitting "?>" gives you more control over control / newline characters at the end of your document.

    Good idea, here you go:

    <?php
      $request = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
      $lower = strtolower($request);
      if($request == $lower) {
        // already lowercase, and file was not found
        include_once('error_404.html'); // your 'file not found'-page
      } else {
        // non-lowercase file not found - redirect to lowercase file
        $query = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
        if($query) { $lower .= '?' . $query; }
        header('HTTP/1.1 301 Moved Permanently');
        header('Location: ' . $lower);
      }
      exit();
    PHP:
    Hope this works, didn't test
    I hereby declare it public domain ;) Enjoy!
     
    keyaa, Nov 5, 2008 IP
  7. kslokesh

    kslokesh Well-Known Member

    Messages:
    153
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #7
    kslokesh, Nov 5, 2008 IP
    keyaa likes this.
  8. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You're welcome :D

    Just to clarify: you can still have uppercase and mixed-case files and directories, they just won't be reachable via random-case, just via their exact case-sensitive filename.

    The script works for directories as well (should be obvious ;))

    If you tried the forum first without getting any good replies, I'll try to help you via PM if I've got time on my hands. (Or just PM me your thread URL and I'll read it and reply there if I can help or state via PM that I can't)

    Good luck with your project!
     
    keyaa, Nov 6, 2008 IP