codeigniter 使用.htaccess 去除網址中的 index.php

行動版 for , 瀏覽人次: 1539  , SSL Connection SSL
  • PHP 的 MVC FrameWork – Codeigniter ,原始安裝完後,網址中會有 index.php , 例如: http://www.xxxxx.com.tw/c/index.php/pages/view/ ,對MVC熟悉的人,會知道從網址可以看出 Control Name 與 Action Name,但是 Codeigniter 怎麼讓網址多出了個 index.php ?

    網路上有很多眾說紛紜的方法,說可以從網址中移除index.php,但是試過後不一定有效,因此提出以下方法,可以試試:

    Image 6

    1. 在 Codeigniter 根目錄加一個.htaccess, 裡面內容如下即可:

    DirectoryIndex index.php

    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index.php|index.html|images|robots.txt|js)
    RewriteRule ^(.*)$ index.php?/$1 [L]

    因為我是把Codeigniter 安裝在 c 資料夾底下, 因此我的 RewriteBase /c
    如果你是安裝在網站根目錄, 則直接寫 RewriteBase /  即可!
    看你是安裝在哪個資料夾, 請自己調整 RewriteBase / 的寫法!

     

    2. 不用去調整config !

    $config['base_url'] = '';
    $config['index_page'] = '';

     

    3. 之後我的網址就變成:http://www.xxxxx.com.tw/c/pages/view/ 囉

回 文章列表頁