about 10 years ago

前面幾篇的練習
輸入網址都是要打 localhost/CodeIgniter/index.php/account/index
中間的 index.php 實在是天殺的醜阿

考慮到URL的美觀、還有未來維護專案的方便性(不想所有的Project都混在CodeIgniter底下)
以下以 Win7+Appserv 的環境來實做

有一個project名稱叫做account,放在AppServ/www底下,只取用本來CodeIgniter的部分資料夾及檔案:

  • AppServ/www/account/
    • application/
    • system/
    • index.php

[Step1] 確定 Apache 的 mod_rewrite 功能有開啟

AppServ/Apache2.2/conf/httpd.conf

  • mod_rewrite 反註解

#LoadModule rewrite_module modules/mod_rewrite.so
=> LoadModule rewrite_module modules/mod_rewrite.so

  • AllowOverride 有三處要改

AllowOverride None
=> AllowOverride All

[Step2] 修改 config.php

AppServ/www/account/application/config/config.php

$config['index_page'] = 'index.php';
=> $config['index_page'] = '';

[Step3] 新增.htaccess

在整個專案的根目錄增加.htaccess
此例中的架構如下

  • AppServ/www/account
    • application/
    • system/
    • index.php
    • .htaccess

.htaccess的內容

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteCond $1 !^(index\.php|images|swf|uploads|js|css|assets|robots\.txt|$)
    RewriteRule ^(.*)$ /account/index.php/$1 [L,QSA]
</IfModule>

RewriteCond $1 !^(index\.php|images|swf|uploads|js|css|assets|robots\.txt|$)
這條是為了不讓mod_rewrite後,檔案裡面的圖、template失效,所需要一併改寫的。

[Step4] 設定完成,重開機

用了好幾次 Appserv 的 Control Server by Service
做 Apache Restart 都一直沒有成功
直接重開機是最快的方式

本來瀏覽器要輸入 localhost/account/index.php/account
現在用 localhost/account/account 就可以囉


以後的專案只要建在 Appserv/www 底下,然後再做Step2、Step3就可以了 (更動Apache設定才需要重開機)

  • Appserv/www
    • account (專案名稱)/
      • application/
      • controllers/
      • account.php (各個Controllers)
    • project1/
    • project2/
    • .
    • .

localhost/專案名稱/Controllers 就可以囉
ex: localhost/account/account


[參考]

win7、ubuntu 去除index.php

← CI 練習- [帳號申請] 表單傳遞 form_open、method CI 練習- [帳號申請] database 連結 →
 
comments powered by Disqus