我在使用Codeigniter2时遇到了一些麻烦。 基本上我想从我的网址中删除烦人的“index.php”,以便:
- http://localhost/ci_intro/index.php/site/home
Becomes
- http://localhost/ci_intro/home
我已经关注了Stack Overflow上的几个链接,并找到了以下两个帖子:
Remove index.php From URL - Codeigniter 2
和
Removing index.php from codeigniter-2
然而,在执行这两个步骤后仍然没有快乐。
我的.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中有两个视图,一个叫做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.但是,如果我在URL中输入“site”,我的控制器的名称,链接会将我带到正确加载的视图:
http://localhost/ci_intro/site/home
or
http://localhost/ci_intro/site/about
是否有任何已知的方法绕过此方法,以便控制器(在本例中为站点)完全从URL中删除,因为在我视图中的链接中使用site / home或site / about似乎不起作用?< / p>
答案 0 :(得分:3)
你可能不想这样做(但可能!)。
- http://localhost/ci_intro/index.php/site/home Becomes - http://localhost/ci_intro/home
CI的工作方式,site
是控制器,home
是您正在调用的控制器中的方法或功能。这允许您收集相应的功能。
e.g。你可以有一个家庭控制器和索引方法,如:
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之类的网址)会显示带有文字&#34的页面;我是家庭控制器索引&#34;。
然后您可以将其他功能添加到家庭控制器a&#39; 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";
}
}
带有http://example.com/home/user
等网址的会显示带有文字&#34的页面;我是家庭控制器中的用户方法&#34;。
您还可以根据需要创建任意数量的其他控制器:
class About extends CI_Controller{
public function index(){
// http://example.com/about
echo 'I am the about page!';
}
}
http://example.com/about
将呈现包含文字&#34的页面;我是关于页面!&#34;
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
答案 1 :(得分:2)
使用路线:http://ellislab.com/codeigniter/user-guide/general/routing.html
看起来像这样:
$route['home'] = 'site/home';
$route['about'] = 'controllername/about';