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.

How do I make Linux server urls case insensitive?

Discussion in 'PHP' started by Imozeb, May 4, 2010.

  1. #1
    When the user types into the address bar: www.mySite.com the user will get an error because linux server urls are case sensitive and the url is www.mysite.com. How would I make it so the urls are case insensitive. I've been trying a PHP script but it doesn't seem to be working (It just takes me to the index directory of the folder and gives a 403 forbiden error.) Is there any code I can place on my site that will make the urls case insensitive?

    Thanks. :)
     
    Imozeb, May 4, 2010 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    In a .htaccess file put
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com$
    RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]
    Code (markup):
    I have to say I've never heard of this occurring before. Most browsers will send the lowercase version of the domain name to the server regardless. Are you sure you don't mean that your file names are case sensitive?
     
    JAY6390, May 4, 2010 IP
  3. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    wha... ? This makes absolutely no sense.
     
    krsix, May 4, 2010 IP
  4. monfis

    monfis Well-Known Member

    Messages:
    1,476
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    160
    #4
    Have the same experience, file names are the "problem", not the domain. Any solution to this via htaccess?
     
    monfis, May 4, 2010 IP
  5. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You could rewrite any files that don't exist to a php script, that then checks for the same file in lowercase. There's nothing that you could do directly with just htaccess however
     
    JAY6390, May 4, 2010 IP
  6. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yes, I meant the filename. But how would I get the filename to my PHP script? I created a PHP script that gets the referer of the page, strtolower() it, and then puts the user through via header() to the lowercased page, but it seems to me that the browser never gets to my script. It gives a 403 forbiden error and the url looks like it is trying to get the file folders like www.mysite.com/script/ How would I get the browser to go to the script if there is an error in the filenames case? I tried using 404 error page but it isn't working. :(
     
    Last edited: May 4, 2010
    Imozeb, May 4, 2010 IP
    Primate likes this.
  7. monfis

    monfis Well-Known Member

    Messages:
    1,476
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    160
    #7
    Thanks for your information, tried to give you some rep but the new DP design is still suffering configuration issues.
     
    monfis, May 4, 2010 IP
  8. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #8
    Hit the little black star icon in the bottom left corner of the post.
     
    danx10, May 4, 2010 IP
  9. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Keep in mind that __NAMES ARE CASE SENSITIVE. IT IS NOT A FAULT OR PROBLEM OF THE SERVER__
    Ask your web host to install the Apache module "mod_speling" (speling, not spelling)
    It will redirect incorrect case to the right lowercase one and correct typos (in a SEO friendly way)
     
    krsix, May 4, 2010 IP
  10. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I contacted my hosting provider and they stated that my hosting account does not allow the Apache module "mod_speling". Any ideas for a work around?

    Thanks.
     
    Imozeb, May 5, 2010 IP
  11. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Imozeb, May 6, 2010 IP
  12. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I was thinking about using a php code that will strtolower the filename and then use header to take the user to the correct page but I can't figure out how to get my PHP file to run if there is an error in the filename. Does anyone know how I could do this?
     
    Imozeb, May 7, 2010 IP
  13. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
  14. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #14
    I checked out that page you posted and I found this.

    .htaccess
    RewriteEngine On 
    RewriteMap  lc int:tolower 
    RewriteCond %{REQUEST_URI} [A-Z] 
    RewriteRule (.*) ${lc:$1} [R=301,L]
    Code (markup):

    I placed this .htaccess file in the same folder that is using the rewrite and turned off word-wrap.
    It is still not working. I am using Godaddy Linux hosting. Could you please tell me why this isn't working? It looks like what I am looking for.

    Thank you.
     
    Imozeb, May 7, 2010 IP
  15. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #15
    OK well since nobody has managed to find a solution to this yet, I thought I'd share with you the method I originally suggested that works. There are two (three possibly) files you will need for this
    1 - The .htaccess file
    
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule .* badRequests.php [L]
    
    Code (markup):
    2 - badRequests.php
    
    <?php
    // get the lowercase version of the URI
    $lower = strtolower($_SERVER['REQUEST_URI']);
    
    // If the URI isn't the same as the lowercase version, redirect to the lowercase version
    if($_SERVER['REQUEST_URI'] !== $lower) {
        header('Location: '.$lower);
        die();
    }else{
        //Otherwise give a 404 error
        header("HTTP/1.0 404 Not Found");
        die();
        
        //You could remove the die() line above of course to allow this to execute
        //This will send the user to the errordocument.php file, where you can show
        //your custom error document file if you wish
        header('Location: /errorDocument.php');
        die();
    }
    
    PHP:
    3 - an optional errorDocument.php file containing your error message
     
    JAY6390, May 7, 2010 IP
  16. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Now the webpage won't even load if I type the url in uppercase. I don't know what's wrong. :confused:

    Here is some information which might help you help me:

    My .htaccess file is saved with the encoding as 'ANSI' if this matters?
    I copied your code exactly into the correct file names.
    My .htaccess file and badRequest.php file are in the same directory which is inside the one that I am trying to fix.

    If you need anymore information please tell me.

    Thank you.
     
    Imozeb, May 7, 2010 IP
  17. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #17
    Imozeb, the code you posted with a RewriteMap directive in it doesn't work because that particular directive isn't allowed in an htaccess file on any Apache server. Each directive has what's called a "context" in which it is available, if when looking at an Apache manual page you don't see .htaccess listed for that directives context, you can't use it in htaccess.

    What also sucks about that in this case, is you can't explicitly use the internal tolower function as a map in RewriteRule like you might think. So something like this, isn't going to work.

    # RewriteRule [A-Z] http://%{HTTP_HOST}${tolower:%{REQUEST_URI}} [R=301,L]
    Code (markup):
    I don't have anything hosted with Godaddy anymore so I can't test this myself, but it works here at home.

    RewriteRule ![A-Z] - [S=26]
    RewriteRule ^(.*)(A)(.*)$ $1a$3 [N,R=301]
    RewriteRule ^(.*)(B)(.*)$ $1b$3 [N,R=301]
    RewriteRule ^(.*)(C)(.*)$ $1c$3 [N,R=301]
    RewriteRule ^(.*)(D)(.*)$ $1d$3 [N,R=301]
    RewriteRule ^(.*)(E)(.*)$ $1e$3 [N,R=301]
    RewriteRule ^(.*)(F)(.*)$ $1f$3 [N,R=301]
    RewriteRule ^(.*)(G)(.*)$ $1g$3 [N,R=301]
    RewriteRule ^(.*)(H)(.*)$ $1h$3 [N,R=301]
    RewriteRule ^(.*)(I)(.*)$ $1i$3 [N,R=301]
    RewriteRule ^(.*)(J)(.*)$ $1j$3 [N,R=301]
    RewriteRule ^(.*)(K)(.*)$ $1k$3 [N,R=301]
    RewriteRule ^(.*)(L)(.*)$ $1l$3 [N,R=301]
    RewriteRule ^(.*)(M)(.*)$ $1m$3 [N,R=301]
    RewriteRule ^(.*)(N)(.*)$ $1n$3 [N,R=301]
    RewriteRule ^(.*)(O)(.*)$ $1o$3 [N,R=301]
    RewriteRule ^(.*)(P)(.*)$ $1p$3 [N,R=301]
    RewriteRule ^(.*)(Q)(.*)$ $1q$3 [N,R=301]
    RewriteRule ^(.*)(R)(.*)$ $1r$3 [N,R=301]
    RewriteRule ^(.*)(S)(.*)$ $1s$3 [N,R=301]
    RewriteRule ^(.*)(T)(.*)$ $1t$3 [N,R=301]
    RewriteRule ^(.*)(U)(.*)$ $1u$3 [N,R=301]
    RewriteRule ^(.*)(V)(.*)$ $1v$3 [N,R=301]
    RewriteRule ^(.*)(W)(.*)$ $1w$3 [N,R=301]
    RewriteRule ^(.*)(X)(.*)$ $1x$3 [N,R=301]
    RewriteRule ^(.*)(Y)(.*)$ $1y$3 [N,R=301]
    RewriteRule ^(.*)(Z)(.*)$ $1z$3 [N,R=301]
    Code (markup):
     
    joebert, May 8, 2010 IP
  18. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #18
    I put your code in a .htaccess file in the directory it is supposed to modify. Is this correct and should I have an index page for this directory because it seems like when I type in the url it deletes the filename and just puts everything up to the slash /.

    It still isn't working. Would a link to my site help or is there any more information I could give?
     
    Imozeb, May 8, 2010 IP
  19. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #19
    I have my test setup in a directory named "/test/", so the path of this htaccess file is "/test/.htaccess".

    This is the entire contents of the htaccess file

    RewriteEngine On
    RewriteBase /test/
    
    RewriteRule ![A-Z] - [S=26]
    RewriteRule ^(.*)(A)(.*)$ $1a$3 [N,R=301]
    RewriteRule ^(.*)(B)(.*)$ $1b$3 [N,R=301]
    RewriteRule ^(.*)(C)(.*)$ $1c$3 [N,R=301]
    RewriteRule ^(.*)(D)(.*)$ $1d$3 [N,R=301]
    RewriteRule ^(.*)(E)(.*)$ $1e$3 [N,R=301]
    RewriteRule ^(.*)(F)(.*)$ $1f$3 [N,R=301]
    RewriteRule ^(.*)(G)(.*)$ $1g$3 [N,R=301]
    RewriteRule ^(.*)(H)(.*)$ $1h$3 [N,R=301]
    RewriteRule ^(.*)(I)(.*)$ $1i$3 [N,R=301]
    RewriteRule ^(.*)(J)(.*)$ $1j$3 [N,R=301]
    RewriteRule ^(.*)(K)(.*)$ $1k$3 [N,R=301]
    RewriteRule ^(.*)(L)(.*)$ $1l$3 [N,R=301]
    RewriteRule ^(.*)(M)(.*)$ $1m$3 [N,R=301]
    RewriteRule ^(.*)(N)(.*)$ $1n$3 [N,R=301]
    RewriteRule ^(.*)(O)(.*)$ $1o$3 [N,R=301]
    RewriteRule ^(.*)(P)(.*)$ $1p$3 [N,R=301]
    RewriteRule ^(.*)(Q)(.*)$ $1q$3 [N,R=301]
    RewriteRule ^(.*)(R)(.*)$ $1r$3 [N,R=301]
    RewriteRule ^(.*)(S)(.*)$ $1s$3 [N,R=301]
    RewriteRule ^(.*)(T)(.*)$ $1t$3 [N,R=301]
    RewriteRule ^(.*)(U)(.*)$ $1u$3 [N,R=301]
    RewriteRule ^(.*)(V)(.*)$ $1v$3 [N,R=301]
    RewriteRule ^(.*)(W)(.*)$ $1w$3 [N,R=301]
    RewriteRule ^(.*)(X)(.*)$ $1x$3 [N,R=301]
    RewriteRule ^(.*)(Y)(.*)$ $1y$3 [N,R=301]
    RewriteRule ^(.*)(Z)(.*)$ $1z$3 [N,R=301]
    Code (markup):
     
    joebert, May 8, 2010 IP
  20. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #20
    It worked!!! Thanks! Just two quick questions 1. How would I create a 404 redirect if the page doesnt exist and 2. Where is the best place to learn .htaccess?

    Thanks again.
     
    Imozeb, May 9, 2010 IP