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
    Code (Text):
    1. RewriteEngine on
    2. RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com$
    3. RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]
    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:
    439
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    wha... ? This makes absolutely no sense.
    krsix, May 4, 2010 IP
  4. monfis

    monfis Active Member

    Messages:
    1,470
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    90
    #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 Active Member

    Messages:
    1,470
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    90
    #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,181
    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:
    439
    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,181
    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
    Code (Text):
    1. RewriteEngine On
    2. RewriteMap  lc int:tolower
    3. RewriteCond %{REQUEST_URI} [A-Z]
    4. RewriteRule (.*) ${lc:$1} [R=301,L]

    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
    Code (Text):
    1.  
    2. RewriteEngine On
    3. RewriteBase /
    4. RewriteCond %{REQUEST_FILENAME} !-f
    5. RewriteRule .* badRequests.php [L]
    6.  
    2 - badRequests.php
    PHP:
    1.  
    2. <?php
    3. // get the lowercase version of the URI
    4. $lower = strtolower($_SERVER['REQUEST_URI']);
    5.  
    6. // If the URI isn't the same as the lowercase version, redirect to the lowercase version
    7. if($_SERVER['REQUEST_URI'] !== $lower) {
    8.     header('Location: '.$lower);
    9.     die();
    10. }else{
    11.     //Otherwise give a 404 error
    12.     header("HTTP/1.0 404 Not Found");
    13.     die();
    14.    
    15.     //You could remove the die() line above of course to allow this to execute
    16.     //This will send the user to the errordocument.php file, where you can show
    17.     //your custom error document file if you wish
    18.     header('Location: /errorDocument.php');
    19.     die();
    20. }
    21.  
    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,152
    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.

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

    Code (Text):
    1. RewriteRule ![A-Z] - [S=26]
    2. RewriteRule ^(.*)(A)(.*)$ $1a$3 [N,R=301]
    3. RewriteRule ^(.*)(B)(.*)$ $1b$3 [N,R=301]
    4. RewriteRule ^(.*)(C)(.*)$ $1c$3 [N,R=301]
    5. RewriteRule ^(.*)(D)(.*)$ $1d$3 [N,R=301]
    6. RewriteRule ^(.*)(E)(.*)$ $1e$3 [N,R=301]
    7. RewriteRule ^(.*)(F)(.*)$ $1f$3 [N,R=301]
    8. RewriteRule ^(.*)(G)(.*)$ $1g$3 [N,R=301]
    9. RewriteRule ^(.*)(H)(.*)$ $1h$3 [N,R=301]
    10. RewriteRule ^(.*)(I)(.*)$ $1i$3 [N,R=301]
    11. RewriteRule ^(.*)(J)(.*)$ $1j$3 [N,R=301]
    12. RewriteRule ^(.*)(K)(.*)$ $1k$3 [N,R=301]
    13. RewriteRule ^(.*)(L)(.*)$ $1l$3 [N,R=301]
    14. RewriteRule ^(.*)(M)(.*)$ $1m$3 [N,R=301]
    15. RewriteRule ^(.*)(N)(.*)$ $1n$3 [N,R=301]
    16. RewriteRule ^(.*)(O)(.*)$ $1o$3 [N,R=301]
    17. RewriteRule ^(.*)(P)(.*)$ $1p$3 [N,R=301]
    18. RewriteRule ^(.*)(Q)(.*)$ $1q$3 [N,R=301]
    19. RewriteRule ^(.*)(R)(.*)$ $1r$3 [N,R=301]
    20. RewriteRule ^(.*)(S)(.*)$ $1s$3 [N,R=301]
    21. RewriteRule ^(.*)(T)(.*)$ $1t$3 [N,R=301]
    22. RewriteRule ^(.*)(U)(.*)$ $1u$3 [N,R=301]
    23. RewriteRule ^(.*)(V)(.*)$ $1v$3 [N,R=301]
    24. RewriteRule ^(.*)(W)(.*)$ $1w$3 [N,R=301]
    25. RewriteRule ^(.*)(X)(.*)$ $1x$3 [N,R=301]
    26. RewriteRule ^(.*)(Y)(.*)$ $1y$3 [N,R=301]
    27. RewriteRule ^(.*)(Z)(.*)$ $1z$3 [N,R=301]
    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,152
    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

    Code (Text):
    1. RewriteEngine On
    2. RewriteBase /test/
    3.  
    4. RewriteRule ![A-Z] - [S=26]
    5. RewriteRule ^(.*)(A)(.*)$ $1a$3 [N,R=301]
    6. RewriteRule ^(.*)(B)(.*)$ $1b$3 [N,R=301]
    7. RewriteRule ^(.*)(C)(.*)$ $1c$3 [N,R=301]
    8. RewriteRule ^(.*)(D)(.*)$ $1d$3 [N,R=301]
    9. RewriteRule ^(.*)(E)(.*)$ $1e$3 [N,R=301]
    10. RewriteRule ^(.*)(F)(.*)$ $1f$3 [N,R=301]
    11. RewriteRule ^(.*)(G)(.*)$ $1g$3 [N,R=301]
    12. RewriteRule ^(.*)(H)(.*)$ $1h$3 [N,R=301]
    13. RewriteRule ^(.*)(I)(.*)$ $1i$3 [N,R=301]
    14. RewriteRule ^(.*)(J)(.*)$ $1j$3 [N,R=301]
    15. RewriteRule ^(.*)(K)(.*)$ $1k$3 [N,R=301]
    16. RewriteRule ^(.*)(L)(.*)$ $1l$3 [N,R=301]
    17. RewriteRule ^(.*)(M)(.*)$ $1m$3 [N,R=301]
    18. RewriteRule ^(.*)(N)(.*)$ $1n$3 [N,R=301]
    19. RewriteRule ^(.*)(O)(.*)$ $1o$3 [N,R=301]
    20. RewriteRule ^(.*)(P)(.*)$ $1p$3 [N,R=301]
    21. RewriteRule ^(.*)(Q)(.*)$ $1q$3 [N,R=301]
    22. RewriteRule ^(.*)(R)(.*)$ $1r$3 [N,R=301]
    23. RewriteRule ^(.*)(S)(.*)$ $1s$3 [N,R=301]
    24. RewriteRule ^(.*)(T)(.*)$ $1t$3 [N,R=301]
    25. RewriteRule ^(.*)(U)(.*)$ $1u$3 [N,R=301]
    26. RewriteRule ^(.*)(V)(.*)$ $1v$3 [N,R=301]
    27. RewriteRule ^(.*)(W)(.*)$ $1w$3 [N,R=301]
    28. RewriteRule ^(.*)(X)(.*)$ $1x$3 [N,R=301]
    29. RewriteRule ^(.*)(Y)(.*)$ $1y$3 [N,R=301]
    30. RewriteRule ^(.*)(Z)(.*)$ $1z$3 [N,R=301]
    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