[英]Removing index.php in Codeigniter 2
使用Codeigniter2时遇到一些麻烦。 本质上,我想从我的网址中删除烦人的“ index.php”,以便:
- http://localhost/ci_intro/index.php/site/home
Becomes
- http://localhost/ci_intro/home
我已经关注了Stack Overflow上的几个链接,并发现了以下两个帖子:
和
但是,执行完两个步骤后仍然没有喜悦。
我的.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>
我确保在Apache中启用了mod_rewrite模块(我在Windows上使用Xampp),但是由于某种原因,它无法正常工作。
我在CodeIgniter2中有两个视图,一个称为“首页”,另一个称为“关于”。 这两个相互链接。 链接如下:
<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。但是,如果我随后在URL(控制器的名称)中键入“ site”,则链接会将我引导至正确加载的视图:
http://localhost/ci_intro/site/home
or
http://localhost/ci_intro/site/about
是否有任何已知的方法可以绕过此方法,以使控制器(在本例中为站点)完全从URL中删除,因为在我看来链接中使用site / home或site / about似乎不起作用?
您可能不想这样做(但是可以!)。
- 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
(或在涉及.htaccess之前为http://example.com/index.php/home
)的网址将显示一个带有文本“我是家庭控制器索引”的页面。
然后,您可以将其他功能添加到家用控制器中
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
的网址会显示一个带有文本“我是家庭控制器中的用户方法”的页面。
您还可以根据需要创建任意数量的其他控制器:
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]
是所有需要从网址中删除index.php的.htaccess文件。 它位于/ci_into/.htaccess
如果您确实要使用与站点控制器一起使用的http://example.com/home ,则可以使用路由
使用路线: http : //ellislab.com/codeigniter/user-guide/general/routing.html
它看起来像这样:
$route['home'] = 'site/home';
$route['about'] = 'controllername/about';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.