【问题标题】:Codeigniter - client posted links with capital letters in URLCodeigniter - 客户端在 URL 中发布带有大写字母的链接
【发布时间】:2011-11-02 12:24:28
【问题描述】:

我的客户以大写字母打印了指向该站点的链接。 我有一个 CI 安装,需要更改 htaccess 或基本控制器(我在 mo 尝试了很多选项)以像这些示例一样工作(用户在他们的地址栏中键入它):

http://www.site.com/HAPPY/chappy -> http://www.site.com/happy/chappy

http://www.SITE.com/HaPpy/CHAPpy -> http://www.site.com/happy/chappy

http://WWW.Site.CoM/happy/chappY -> http://www.site.com/happy/chappy

等等…… 我似乎无法让 htaccess 简单地说“把所有东西都录进去,把它变成小写然后处理它”

这有可能吗..?

【问题讨论】:

    标签: .htaccess codeigniter lowercase


    【解决方案1】:

    是的,您可以在输入文本后立即将输入转换为小写字母。你可以用javascript来做到这一点。请参阅下面的代码狙击。

    function makeLowercase() {
    document.form1.outstring.value = document.form1.instring.value.toLowerCase();
    }
    
    <input name="outstring" type="text" value="" size="30" onkeyup="makeLowercase();" onblur="makeLowercase();">
    

    或者您可以使用 PHP 中的 strtolower() 函数将数据转换为小写并进行处理。

    【讨论】:

    • 干杯..我确实把这个放在我灌输的索引页中:$_SERVER['REQUEST_URI'] = strtolower($_SERVER['REQUEST_URI']);没有变化..上面所有的例子仍然都是404页,而不是重写。我是猴子吗……这不是正确的方法吗?
    • 不,它不工作.. 根本什么都不做...仅供参考,当用户使用新的浏览器并输入网址时
    • 这是 javascript,不能解决上述问题。除了设置 404_override 并创建一个控制器,该控制器基本上可以执行我在单独答案中所说的操作之外,更改路由配置也无济于事。
    【解决方案2】:

    您可以使用 .htaccess 重写主机名之后的部分(无论如何,该部分不区分大小写):

    http://www.chrisabernethy.com/force-lower-case-urls-with-mod_rewrite/

    【讨论】:

    • 将这个:code`` RewriteMap lc int:tolower RewriteCond %{REQUEST_URI} [A-Z] RewriteRule (.*) ${lc:$1} [R=301,L] code 放入我的 htaccess 文件会导致站点因服务器错误而失败
    • 删除此行RewriteMap lc int:tolower 删除服务器错误...此行到底在做什么?
    【解决方案3】:

    Extend the CI_Exceptions core class 与您自己的 show_error 函数。使用show_error 的原始代码,但在开头添加一个快速检查:

    class MY_Exceptions extends CI_Exceptions {
    
        /**
         * General Error Page
         *
         * This function takes an error message as input
         * (either as a string or an array) and displays
         * it using the specified template.
         *
         * @access  private
         * @param   string  the heading
         * @param   string  the message
         * @param   string  the template name
         * @return  string
         */
        function show_error($heading, $message, $template = 'error_general', $status_code = 500)
        {
            // First try forwarding to all-lowercase URL if there were caps in the request
            if ($_SERVER['REQUEST_URI'] != strtolower($_SERVER['REQUEST_URI']))
            {
                header('Location: ' . strtolower($_SERVER['REQUEST_URI']));
                return;
            }
            /* Rest of original function to follow... */
    

    根据需要进行自定义,记住 URI 的其他部分可能需要大写。

    您也可以使用 pre_system (or pre_controller) hook 实现类似的功能,不过我个人只会在已经为其他目的启用挂钩的情况下这样做。

    【讨论】:

      【解决方案4】:

      您可以将核心路由库扩展为不区分大小写。这就是你放置 strtolower() 函数的地方。

      将其添加到 404 错误处理程序是一个坏主意。

      如果要源码,LMK,不过应该是扩展库,复制1个函数,加上strtolower()

      【讨论】:

        猜你喜欢
        • 2017-07-17
        • 2022-12-06
        • 2018-08-12
        • 1970-01-01
        • 1970-01-01
        • 2019-11-21
        • 1970-01-01
        • 2015-12-15
        • 2011-06-27
        相关资源
        最近更新 更多