2011-04-08 94 views
0

我在使用.htaccess一次性执行这两个操作时遇到了麻烦。如何重写URLS以删除index.php并用codeigniter强制小写?

删除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] 

回答

0

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

  1. 删除的index.php仍发送所有请求,因此请求的URI仍然有index.php文件中它,即使你没有看到它或将它添加到URL中的index.php/yadda/yadda。
  2. 重定向(由第二重写规则触发)空值索引部分,所以你会再拿到mysite.com/index.php/lowercase/

但是,除了这一切RewriteMap指令只能在你的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); 
} 

这我把你的类的主要结构,我希望这可以帮助你。

1

如果您只是试图强制小写,建议使用的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 

讨论在这里找到Force lowercase .htaccess