CI URI有效刪除index.php(完善CI手冊)

       秉承MVC架構的思想,CI中的全部控制器都須要通過單點入口文件index.php(默認)來加載調用。也就是說,在默認狀況下,全部CI開發項目的URL都形如如下這種形式:php

http://localhost/index.php/welcome很顯然,默認狀況下,index.php在URL地址段中的存在必定程度上影響了URL的簡潔和SEO的進行。咱們能夠經過下面本文介紹的方法來去掉這個討厭的Index.php。nginx

你或許已經注意到在CodeIgniter用戶手冊中,已經存在關於此問題的解決方法。但官方提供的這個.htaccess配置,並非全部時候都能解決問題。下面通過筆者調試操做步驟供你們分享:
前提:服務器配置使用Apache做爲Web服務器。
apache

一、將如下配置信息複製並保存爲.htaccess文件(.htaccess文件放置在根目錄下,即與application目錄同級),服務器

默認狀況下,index.php 文件將被包含在你的 URL 中:架構

 example.com/index.php/news/article/my_article
app

你能夠很容易的經過 .htaccess 文件來設置一些簡單的規則刪除它。ide

-----------------------------------------------------------------網站

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

 

注意:若是你的項目不在根目錄請把上面這一句改成:RewriteRule ^(.*)$ index.php/$1 [L] 在上面的例子中,能夠實現任何非index.php、images 和 robots.txt 的 HTTP 請求都被指向 index.php。spa

(第一步操做與CI操做手冊中相同)

二、修改完成後咱們訪問CI默認歡迎頁面(http://localhost/welcome)時,會報錯,錯誤信息以下:

--------------------------------------------------------------

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, admin@sky00.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log. 

 --------------------------------------------------------------------------------------

我看了下這裏網站裏的全部東西都是這樣報錯,就是圖片和TXT也打不開,而後查看了一下程序發現裏面有三個文件.htaccess、httpd.ini、nginx.conf,我想應該是這幾個傢伙在做怪,首先想到的就是.htaccess這個分佈文件,將其刪除,果斷打開網站了,那麼找到緣由了,把刪除了的找回來,去apache裏開啓.htaccess就行了,下面是開啓方法:

在apache配置文件httpd.conf中找到

Options FollowSymLinks
AllowOverride None

有的版本是這樣寫的

Options FollowSymLinks ExecCGI Indexes
AllowOverride None

總之無論怎麼寫,你只須要把下面的

AllowOverride None改成AllowOverride All

而後在找到

LoadModule rewrite_module modules/mod_rewrite.so

把這個前面的「#」號去掉,就是去掉註釋,而後重啓apache.搞定!

相關文章
相關標籤/搜索