Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess 如何重写URL以删除index.php并使用codeigniter强制使用小写?_.htaccess_Codeigniter_Mod Rewrite - Fatal编程技术网

.htaccess 如何重写URL以删除index.php并使用codeigniter强制使用小写?

.htaccess 如何重写URL以删除index.php并使用codeigniter强制使用小写?,.htaccess,codeigniter,mod-rewrite,.htaccess,Codeigniter,Mod Rewrite,我无法使用.htaccess同时执行这两个操作 删除index.php是可行的,但尝试添加强制小写会引发500错误。我正在使用codeigniter。感谢您的帮助 这是我的.htaccess代码: RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?$1 RewriteMap lc int:tolower Re

我无法使用.htaccess同时执行这两个操作

删除index.php是可行的,但尝试添加强制小写会引发500错误。我正在使用codeigniter。感谢您的帮助

这是我的.htaccess代码:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1

RewriteMap  lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L]

那是因为这是不可能的,让我解释一下

  • 删除index.php仍然会将所有请求发送到index.php/yadda/yadda,因此请求的uri中仍然包含index.php,即使您没有看到它或将它添加到url中
  • 重定向(由第二条重写规则触发)使索引部分为空,这样您就可以得到mysite.com/index.php/lowercase/
  • 但是除了所有这些重写映射只能在Httpd.conf文件中声明之外,您还可以在其中声明它:

    RewriteMap  lc int:tolower
    
    然后在.htaccess文件中使用变量lc,但同样,这两个变量中只有一个会赢,而不能同时拥有这两个变量

    您可以使用小写URL,也可以在不使用index.php的情况下让站点正常工作,因为它们总是会发生冲突,这是因为它们各自的工作性质不同

    实际上,我能看到它发生的唯一方式是在php中,如下所示:

    $this->load->helper('url');
    $your_URL = uri_string();
    preg_match_all('/[A-Z]/', $your_URL, $match) ;
    $total_count = count($match [0]);
    if($total_count > 0){
        $new_line = strtolower($your_URL);
        redirect($new_line);
    }
    

    我会把它放在你的类的主要结构中,我希望这对你有所帮助

    如果您只是试图强制使用小写,则建议的php代码不起作用。下面是正确的php代码

    $url = $_SERVER['REQUEST_URI'];
    $pattern = '/([A-Z]+)/';
    
    if(preg_match($pattern, $url)) {
        $new_url = strtolower($url);
    
        Header( 'HTTP/1.1 301 Moved Permanently' );
        Header( 'Location: ' . $new_url );
        exit;
    }
    
    // your code here
    
    这里有讨论