使用 .htaccess 文件删除 CodeIgniter 中的 index.php [英] Removing index.php in CodeIgniter using .htaccess file
问题描述
我目前正在使用 CodeIgniter 开发一个网站,最近我偶然发现了一个路由 &.htaccess 问题.
I'm currently developing a website using CodeIgniter and I recently stumbled upon a routing & .htaccess problem.
基本上,我的简单网站的结构(为了简单起见,我们称我的项目为CIExample")如下:
Basically, the structure of my simple website (let's call my project 'CIExample' for the sake of simplicity) is as follow:
-首页
-关于我们
-我们的服务
-新闻
-联系我们
-Home
-About Us
-Our Service
-News
-Contact Us
这是我使用 Page 控制器实现的.这个控制器有 5 个函数调用各自的页面视图,即:
which I implemented using a Page controller. This controller has 5 functions which called respective page's view, i.e:
Home(),它调用视图Home"
About(),它调用视图关于我们"等等..
Home(), which calls the view 'Home'
About(), which calls the view 'About Us' and so on..
以前,要访问主页",我需要在 url 窗口中键入 http://localhost/CIExample/index.php/page/home
,但在设置以下路由后,我能够删除页面"(类名)部分:
Previously, to access 'Home', I would need to type http://localhost/CIExample/index.php/page/home
into the url window, but after setting the following routes I was able to remove the 'page' (classname) part:
$route['default_controller'] = 'page/home';
$route['home'] = 'page/home';
$route['about'] = 'page/about';
$route['service'] = 'page/service';
$route['news'] = 'page/news';
$route['contact'] = 'page/contact';
然而,当我尝试删除index.php"时,棘手的部分出现了.我希望能够通过键入 http://localhost/CIExample/home
来访问主页.
However, the tricky part came when I try to remove the 'index.php'.
I want to be able to access the home page by typing http://localhost/CIExample/home
.
于是我在CI论坛/教程/堆栈溢出上做了很多搜索,找到了一些.htaccess文件的代码:
So I did a lot of searches on CI forum/tutorial/stack overflow, and found some codes for .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index.php|images|robots.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
或http://ellislab.com/forums/viewthread/197675/#929904一>
我尝试了两个代码,但没有一个工作..
or http://ellislab.com/forums/viewthread/197675/#929904
I tried both codes but none works..
http://localhost/CIExample/home
会引导我到 404 not found 页面,但 http://localhost/CIExample/index.php/home
会工作得很好.
http://localhost/CIExample/home
would direct me to 404 not found page, but http://localhost/CIExample/index.php/home
would work just fine.
我想知道哪里出了问题?是我的 routes.php 或 .htaccess 还是两者兼而有之?谢谢.
I wonder what went wrongs? Is it my routes.php or .htaccess or both? Thanks.
注意:我也改变了我的 'config/config.php' 文件 -> $config['index_page'] = '';
Note: I've also changed my 'config/config.php' file -> $config['index_page'] = '';
终于成功了!调整 config 文件夹中的 config.php 文件并设置以下 $config['uri_protocol'] = 'PATH_INFO';
(来自 'AUTO').
Finally it works!
After tweaking the config.php file in config folder and set the following $config['uri_protocol'] = 'PATH_INFO';
(from 'AUTO').
可用值:
| 'AUTO' Default - auto detects
| 'PATH_INFO' Uses the PATH_INFO
| 'QUERY_STRING' Uses the QUERY_STRING
| 'REQUEST_URI' Uses the REQUEST_URI
| 'ORIG_PATH_INFO' Uses the ORIG_PATH_INFO
不知道有什么不同,但根据 http://www.jotorres.com/2011/12/removing-index-php-from-url-in-codeigniter/,相同的代码可能/可能无法在生产服务器中工作.
Dunno what's the different but according to one of the comments in http://www.jotorres.com/2011/12/removing-index-php-from-url-in-codeigniter/, the same code might/might not work in the production server.
谁能解释一下原因?
推荐答案
您只需要将其粘贴到您的 .htaccess 文件中:
You just need to paste this in your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
并对您的 application/config/config.php 文件进行此更改:
and make this alternation to your application/config/config.php file:
$config['index_page'] = '';
这篇关于使用 .htaccess 文件删除 CodeIgniter 中的 index.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!