简体   繁体   English

在Codeigniter 2中删除index.php

[英]Removing index.php in Codeigniter 2

I'm having some trouble using Codeigniter2. 使用Codeigniter2时遇到一些麻烦。 Essentially I want to remove the annoying "index.php" from my url so that: 本质上,我想从我的网址中删除烦人的“ index.php”,以便:

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

    Becomes

    - http://localhost/ci_intro/home

I've followed a couple of the links on Stack Overflow already and have found the following two posts: 我已经关注了Stack Overflow上的几个链接,并发现了以下两个帖子:

Remove index.php From URL - Codeigniter 2 从URL删除index.php-Codeigniter 2

And

Removing index.php from codeigniter-2 从codeigniter-2删除index.php

However after performing both steps still no joy. 但是,执行完两个步骤后仍然没有喜悦。

My .htaccess is as follows: 我的.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>   

I've ensured that the mod_rewrite module is enable in Apache (I'm using Xampp on Windows) but for some reason it's just not working as i'd expect it to. 我确保在Apache中启用了mod_rewrite模块(我在Windows上使用Xampp),但是由于某种原因,它无法正常工作。

I have two views in CodeIgniter2, one called Home and another called About. 我在CodeIgniter2中有两个视图,一个称为“首页”,另一个称为“关于”。 These two link to each other. 这两个相互链接。 The links are as follows: 链接如下:

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

If I go to the root URL of my project: 如果我转到项目的根URL:

   http://localhost/ci_intro/

The index function of the Home page is displayed. 显示主页的索引功能。 That's fine and as expected. 很好,符合预期。 However, if I then click either link it redirects to: 但是,如果我单击任一链接,它将重定向到:

   http://localhost/ci_intro/home

   or

   http://localhost/ci_intro/about

Which produces a 404. However if I then type "site" in the URL, the name of my controller, the links take me through to the correctly loaded views: 这将产生404。但是,如果我随后在URL(控制器的名称)中键入“ site”,则链接会将我引导至正确加载的视图:

    http://localhost/ci_intro/site/home

    or

    http://localhost/ci_intro/site/about

Is there any known way of bypassing this approach so that the controller,in this case site, is removed from the URL entirely as using site/home or site/about in the links in my view does not appear to work? 是否有任何已知的方法可以绕过此方法,以使控制器(在本例中为站点)完全从URL中删除,因为在我看来链接中使用site / home或site / about似乎不起作用?

You probably don't want to do that (but might!). 您可能不想这样做(但是可以!)。

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

The way CI works, site is the controller and home is the method or function in the controller you are calling. CI的工作方式, site是控制器, home是您要调用的控制器中的方法或函数。 This allows you to gather respective functionality. 这使您可以收集各自的功能。

eg you could have a home controller and an index method like: 例如,您可能有一个家庭控制器和一个索引方法,例如:

class Home extends CI_Controller{

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

which with a url like http://example.com/home (or http://example.com/index.php/home before .htaccess is involved) would show a page with the text "I am the home controller index". 网址为http://example.com/home (或在涉及.htaccess之前为http://example.com/index.php/home )的网址将显示一个带有文本“我是家庭控制器索引”的页面。

Then you can add other functionality to the home controller a'la 然后,您可以将其他功能添加到家用控制器中

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";
    }
}

which with a url like http://example.com/home/user would show a page with the text "I am the user method in the home controller". 网址为http://example.com/home/user的网址会显示一个带有文本“我是家庭控制器中的用户方法”的页面。

You can also create as many other controllers as you need: 您还可以根据需要创建任意数量的其他控制器:

class About extends CI_Controller{

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

http://example.com/about will render a page with the text "I am the about page!" http://example.com/about将呈现一个带有文本“我是关于页面!”的页面。

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

is all the .htaccess you should need to remove index.php form the url. 是所有需要从网址中删除index.php的.htaccess文件。 It'd live in /ci_into/.htaccess 它位于/ci_into/.htaccess

If you really want to use http://example.com/home that works with the site controller, you could use routing 如果您确实要使用与站点控制器一起使用的http://example.com/home ,则可以使用路由

Use routes: http://ellislab.com/codeigniter/user-guide/general/routing.html 使用路线: http : //ellislab.com/codeigniter/user-guide/general/routing.html

It will look something like this: 它看起来像这样:

$route['home'] = 'site/home';
$route['about'] = 'controllername/about';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM