2013-04-25 196 views
1

我在使用Codeigniter2時遇到了一些麻煩。 基本上我想,這樣從我的網址去掉惱人的「的index.php」:在Codeigniter 2中刪除index.php

- http://localhost/ci_intro/index.php/site/home 

    Becomes 

    - http://localhost/ci_intro/home 

我跟着一對夫婦已經在堆棧溢出的鏈接,並已發現以下兩個職位:

Remove index.php From URL - Codeigniter 2

而且

Removing index.php from codeigniter-2

執行後,但是能夠對S teps依然沒有喜樂。

我的.htaccess如下:

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase /ci_intro/ 

    #Removes access to the system folder by users. 
    #Additionally this will allow you to create a System.php controller, 
    #previously this would not have been possible. 
    #'system' can be replaced if you have renamed your system folder. 
RewriteCond %{REQUEST_URI} ^system.* 
RewriteRule ^(.*)$ /ci_intro/index.php?/$1 [L] 

    #When your application folder isn't in the system folder 
    #This snippet prevents user access to the application folder 
    #Submitted by: Fabdrol 
    #Rename 'application' to your applications folder name. 
RewriteCond %{REQUEST_URI} ^application.* 
RewriteRule ^(.*)$ /ci_intro/index.php?/$1 [L] 

    #Checks to see if the user is attempting to access a valid file, 
    #such as an image or css document, if this isn't true it sends the 
    #request to index.php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /ci_intro/index.php?/$1 [L] 

</IfModule> 

<IfModule !mod_rewrite.c> 
    # If we don't have mod_rewrite installed, all 404's 
    # can be sent to index.php, and everything works as normal. 
    # Submitted by: ElliotHaughin 

    ErrorDocument 404 /index.php 
</IfModule> 

我保證mod_rewrite的模塊是在Apache中啓用(我使用的XAMPP在Windows上),但由於某種原因,它只是不工作,我」 d期待它。

我在CodeIgniter2中有兩個視圖,一個叫做Home,另一個叫About。這兩個鏈接彼此。鏈接如下:

<a href="home">Home</a><br/> 
    <a href="about">About</a> 

如果我去我的項目的根URL:

http://localhost/ci_intro/ 

主頁的索引功能顯示。這很好,和預期的一樣。但是,如果我再單擊鏈接重定向到:

http://localhost/ci_intro/home 

    or 

    http://localhost/ci_intro/about 

其產生的404。但是,如果我再在網址欄中輸入「地盤」,我的控制器的名稱,鏈接帶我穿越到正確加載的觀點:

http://localhost/ci_intro/site/home 

    or 

    http://localhost/ci_intro/site/about 

有這麼控制器,在這種情況下網站,從網址中移除完全是作爲使用網站/家庭或網站/在我看來,鏈接有關繞過這種方法的任何已知的方式似乎沒有工作?

回答

3

你可能不想這樣做(但可能!)。

- http://localhost/ci_intro/index.php/site/home 

Becomes 

- http://localhost/ci_intro/home 

的方式CI工程,site是控制器和home是您所呼叫的控制方法或功能。這使您可以收集各自的功能。

例如你可以有一個家居控制器和索引方法,如:

class Home extends CI_Controller{ 

    public function index(){ 
     echo 'I am the home controller index'; 
    } 
} 

與像http://example.com/home(或http://example.com/index.php/home之前的.htaccess參與)的URL將顯示帶有文本頁面「我是家裏控制器指數」。

然後,你可以給家庭控制器添加其他功能網HRS歐洲

class Home extends CI_Controller{ 

    public function index(){ 
     // http://example.com/home 
     echo 'I am the home controller index'; 
    } 

    public function user(){ 
     // http://example.com/home/user 
     echo "I am the user method in the home controller"; 
    } 
} 

與像http://example.com/home/user一個URL會顯示一個頁面文本「我在家裏控制器的用戶的方法。」

,因爲你需要還可以創建許多其他的控制器:

class About extends CI_Controller{ 

    public function index(){ 
     // http://example.com/about 
     echo 'I am the about page!'; 
    } 
} 

http://example.com/about將呈現頁面與文本「我關於頁面!」

RewriteEngine on 
RewriteCond $1 !^(index\.php|images|robots\.txt) 
RewriteRule ^(.*)$ index.php/$1 [L] 

是你需要從url中刪除index.php所需的所有.htaccess。它會住在/ci_into/.htaccess

如果你真的想用http://example.com/home與現場控制器的工作原理,你可以使用routing

+0

感謝您的回覆。我明白你從URL中刪除控制器的意思,因爲它可能會導致在開發過程中丟失範圍。但是,如果是這種情況,我該如何去關聯我的觀點?如果我使用網站/家庭或網站/關於這是第一次罰款,我點擊其中一個,但下一次它鏈接到網站/網站/約。 – jezzipin 2013-04-25 14:07:46

+1

檢查application/config.php中的$ config ['base_url']'並設置它。 – stormdrain 2013-04-25 14:10:11

+0

在這種情況下,我猜這是localhost/ci_intro? – jezzipin 2013-04-25 14:11:39