0

I've just realized my host URL are case-sensitive.

It means that /homepage, /Homepage and /homePage are different URLs, which is problematic.

I could force any URL to be lower-case and it would fix my problem but this is not exactly what I'm looking for.

I would like to know if I can redirect any variant of /homepage (ie:/hOMEpaGe,/HOMEPAGE,/hOmEpAgE,...) to /homePage (Note the case-sensivity)

I don't know if I'd better handle it with the server configuration or into the php file by manipulating the $_SERVER[REQUEST_URI].

Thank you for any idea on this !

  • See the answer here: http://stackoverflow.com/questions/14814419/how-do-i-make-urls-case-insensitive-in-linux-server – KIKO Software Jun 25 '16 at 10:38
  • "which is problematic" — Why? Do a lot of people type URLs to your site instead of clicking on links and have uncontrollable urges to hit the shift key? Path segments in URLs on most sites are case sensitive, it isn't usually a problem. – Quentin Jun 25 '16 at 10:42

3 Answers3

0

Sure it might be possible to do a redirect on the Bootstrap file (index.php for example) with the combination of strtolower, stristr(), str_replace() and header() Functions like so:

<?php
    // ASSUMING THE BASE URI IS KNOWN
    $baseURI    = "http://www.my-domain.com";
    // CONVERT ALL CHARACTERS IN THE $_SERVER['REQUEST_URI'] TO LOWER-CASES
    $uri        = strtolower($_SERVER['REQUEST_URI']);
    $rdURI      = str_replace("homepage", "homePage", $uri);


    // USE EITHER A SWITCH OR IF CONDITIONAL LOGIC TO SET THE URI
    if(stristr($uri, "homepage")){  // REDIRECT ANY THING WITH HOMEPAGE TO: homePage
        $rdURI  = str_replace("homepage", "homePage", $uri);
    }else if(stristr($uri, "another-uri-1")){
        $rdURI  =  str_replace("another-uri-1", "another-URI-1", $uri);
    }else if(stristr($uri, "another-uri-2")){
        $rdURI  = str_replace("another-uri-2", "another-URI-2", $uri);
    }else if(stristr($uri, "yet-another-uri")){
        $rdURI  = str_replace("yet-another-uri", "yet-Another-URI", $uri);
    }else{
        // DEFAULT TO THE HOMEPAGE IF ALL ELSE FAIL
        // THIS DECISION IS UP TO YOU AS YOU MIGHT NOT NEED IT TO WORK THIS WAY...
        $rdURI  = str_replace("homepage", "homePage", $uri);
    }

    // REDIRECT TO THE NEW URI...
    header("location: " . $rdURI);
    exit;

    // OR REDIRECT TO THE NEW URI WITH BASE URI...
    header("location: " . $baseURI . DIRECTORY_SEPARATOR . $rdURI);
    exit;
Poiz
  • 7,611
  • 2
  • 15
  • 17
0

It is always a better idea to handle this on a lower level than having redirects in PHP.

If order to achieve that, first you need to declare a RewriteMap

# Add RewriteMap for redirecting to lowercase URIs
<IfModule mod_rewrite.c>
RewriteMap lc int:tolower
</IfModule>

Then just create the RewriteRule to redirect uppercase URLs to lowercase ones.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^(.*)$ ${lc:$1} [R=301,L]
Tosho Trajanov
  • 790
  • 8
  • 19
-1

Use .htaccess [NC] for case insensitive:

RewriteCond %{REQUEST_URI} /phpMyAdmin [NC]

lmgtfy: https://perishablepress.com/case-insensitive-redirectmatch/

Cristo
  • 700
  • 1
  • 8
  • 20