问题描述
我目前正在开发一个网站使用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
这是我实现使用页面控制器。该控制器有5个功能,要求各网页的观点,即:
which I implemented using a Page controller. This controller has 5 functions which called respective page's view, i.e:
首页(),它调用视图'家'
关于(),它调用视图关于我们等。
Home(), which calls the view 'Home'
About(), which calls the view 'About Us' and so on..
previously,访问家,我需要键入的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://本地主机/ CIExample /家庭
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论坛/教程/堆栈溢出的搜索,并发现了一些codeS的.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一>
我想这两个codeS,但没有工作。
or http://ellislab.com/forums/viewthread/197675/#929904
I tried both codes but none works..
的http://本地主机/ CIExample /家庭
将直接我找不到404页,但的http://本地主机/ CIExample /指数.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.php文件文件 - > $配置['index_page'] ='';
Note: I've also changed my 'config/config.php' file -> $config['index_page'] = '';
编辑:最后,它的工作原理!在调整配置文件夹中的config.php文件,后设置以下 $配置['uri_protocol'] ='PATH_INFO';
(从'自动')
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-$c$cigniter/ ,同样code可能/可能不会在生产服务器上的工作。
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.
任何人都可以或许说明理由?
Anyone can explain the reason maybe?
推荐答案
您只需要粘贴在你的.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>
和使这个交替到您的应用程序/配置/ config.php文件:
and make this alternation to your application/config/config.php file:
$config['index_page'] = '';
这篇关于使用.htaccess文件在codeIgniter删除的index.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!