Codeigniter语言 - 客户端张贴链接与大写字母在URL



我的客户以大写字母打印了该网站的链接。我有一个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简单地说"接收所有内容,将其转为小写然后处理它"

这是可能的吗?

是的,您可以在输入文本后立即将输入转换为小写字母。你可以用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()函数将数据转换为小写并进行处理。

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

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

用自己的show_error函数扩展CI_Exceptions核心类。使用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(或pre_controller)钩子来实现类似的功能,尽管我个人只在钩子已经为其他目的启用时才会这样做。

可以扩展核心路由库,使其不区分大小写。这就是你要放strtolower()函数的地方。

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

如果您想要源代码,请选择LMK,但这应该是扩展库,复制1个函数,并添加strtolower()的问题。

最新更新