2013-04-25 194 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