簡體   English   中英

如何重寫URL以刪除index.php並使用codeigniter強制小寫?

[英]How to rewrite URLS to remove index.php AND force lowercase with codeigniter?

我無法通過.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]

如果您只是想強制轉換為小寫字母,則建議的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

在這里找到討論強制小寫.htaccess

那是因為不可能,讓我解釋一下。

  1. 刪除index.php仍會將所有請求發送到index.php / yadda / yadda,因此,即使您看不到它,也未在URL中添加了所請求的uri,但其中仍具有index.php。
  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);
}

我將其放入您的類的主要構造中,希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM