处理类似的URL而不会失去案例敏感性

时间:2016-06-25 10:35:23

标签: php apache url-rewriting

我刚刚意识到我的主机网址区分大小写

这意味着/homepage/Homepage/homePage是不同的网址,这是有问题的。

我可以强制任何网址为小写,这样可以解决我的问题,但这并不是我正在寻找的。

我想知道我是否可以将/homepage的任何变体(即:/hOMEpaGe/HOMEPAGE/hOmEpAgE,...)重定向到 { {1}}(注意案例敏感度)

我不知道我是否可以通过操作/homePage来更好地处理服务器配置或php文件。

感谢您对此有任何想法!

3 个答案:

答案 0 :(得分:0)

当然可以使用strtolowerstristr()str_replace()header()的组合对Bootstrap文件(例如index.php)进行重定向像这样的功能:

<?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;

答案 1 :(得分:0)

与在PHP中重定向相比,处理这个问题总是更好。

如果为了达到这个目的,首先需要声明一个RewriteMap

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

然后创建RewriteRule以将大写URL重定向为小写URL。

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

答案 2 :(得分:-1)

使用.htaccess [NC]不区分大小写:

RewriteCond %{REQUEST_URI} /phpMyAdmin [NC]

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

相关问题