2013-03-18 80 views
3

删除的index.php CI中我使用笨目前正在开发一个网站,我最近在一个路由&的.htaccess问题绊倒了。使用.htaccess文件

基本上,我简单的网站结构(我们称之为我为简单起见项目“CIExample”)是如下:

- 首页
- 关于我们
- 我们的服务
- 新闻
- 联系我们

我使用页面控制器实现。该控制器具有5个功能,要求各网页的观点,即:

首页(),它调用视图“家”
关于(),它调用视图“关于我们”等..

此前,访问“家”,我需要http://localhost/CIExample/index.php/page/home键入的URL窗口,但设置了以下路线后,我才得以删除“页”(类名)部分:

$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访问主页。

所以,我做了很多CI论坛/教程/堆栈溢出的搜索,并发现.htaccess文件的一些代码:

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
我想这两个代码,但没有工作..

http://localhost/CIExample/home将指导我未找到404页,但http://localhost/CIExample/index.php/home会工作得很好。

我不知道哪里出了冤屈?是我的routes.php或.htaccess或两者? 谢谢。

注:我也改变了我的 '配置/ config.php文件' 文件 - >$config['index_page'] = '';

编辑: 最后,它的工作原理! 调整在config文件夹config.php文件和设置后,以下$config['uri_protocol'] = 'PATH_INFO';(从“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/一个评论,同样的代码可能/可能不会在生产服务器工作。

任何人都可以或许说明理由?

+0

来自CI论坛的代码应该可以工作。这就是我使用的。您可能需要定义基础,因为您位于子目录中 - RewriteBase/CIExample/ – 2013-03-18 16:46:49

+0

请标记帮助您的答案。它有助于作者和观众。 – 2015-10-19 15:41:19

回答

1

我使用这个htaccess的为我所有的笨项目。它也支持子文件夹。

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase/

    #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 ^(.*)$ /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 ^(.*)$ /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 ^(.*)$ 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> 
+0

是的,这个也适用于我调整config.php文件在config文件夹(见上面的编辑)。但是,谢谢队友:) – Stan 2013-03-19 03:52:14

12

你只需要将它粘贴到你的。htaccess文件:

<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文件:

$config['index_page'] = ''; 
+0

这解决了我的问题。 – 2014-04-11 07:58:03

+0

这也解决了我的问题。谢谢! – Katzumi 2014-06-06 11:33:08

2

我有使用下面的方法,它是在UBUNTU正常使用...! !

首先验证您的index.php文件中是否启用了mod_rewrite(您可以使用phpinfo()进行验证)。

如果禁用了mod_rewrite然后运行: 1. sudo a2enmod rewrite 2. sudo gedit /etc/apache2/sites-available/defaut 只需更换有 “AllowOverride无” 到 “AllowOverride All” 的标签(< '目录/ var/WWW /'>) 3.然后请运行sudo service apache2 restart

好啊好啊...... 现在笨的根目录 - >查找.htaccess文件,如果没有找到,然后按CTRL + H,仍然没有找到,然后创建一个文件.htaccess

.htaccess 粘贴下面的代码:

<IfModule mod_rewrite.c> 
     RewriteEngine On 
     RewriteBase /ROOT DIRECTORY NAME/ 

     # Directs all EE web requests through the site index file 
     RewriteCond %{REQUEST_FILENAME} !-f 
     RewriteCond %{REQUEST_FILENAME} !-d 
     RewriteRule ^(.*)$ index.php/$1 [L] 
</IfModule> 

好最后只需更换ROOT DIRECTORY NAME

DONT忘记REMOVEindex.phpapplication/config/config.php文件 做出这样 $config['index_page'] = '';

-1

该行你只需要将其粘贴到.htaccess文件中(pr oject目录/的.htaccess):

注: 之前进行任何更改,请检查是否“mod_rewrite的”启用与否,如果没有启用试@ IRFU的方式来启用它。

2.确保这是你的项目的根目录内的文件中添加以下代码,但没有一个是已经存在,即应用/的.htaccess

<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文件文件:

$config['index_page'] = ''; 
+0

您对上述答案没有任何区别。请不要那样做。 – MarcoZen 2017-06-18 07:50:39

0
for Codeigniter 3.1.4 

# php -- BEGIN cPanel-generated handler, do not edit 
# Set the “ea-php56” package as the default “PHP” programming language. 
<IfModule mime_module> 
    AddType application/x-httpd-ea-php56 .php .php5 .phtml 
</IfModule> 
# php -- END cPanel-generated handler, do not edit 

# BEGIN cPanel-generated php ini directives, do not edit 
# Manual editing of this file may result in unexpected behavior. 
# To make changes to this file, use the cPanel MultiPHP INI Editor (Home >> Software >> MultiPHP INI Editor) 
# For more information, read our documentation (https://go.cpanel.net/EA4ModifyINI) 
<IfModule php5_module> 
    php_flag asp_tags On 
    php_flag display_errors On 
    php_value max_execution_time 30 
    php_value max_input_time 60 
    php_value max_input_vars 1000 
    php_value memory_limit 512M 
    php_value post_max_size 128M 
    php_value session.gc_maxlifetime 1440 
    php_value session.save_path "/var/cpanel/php/sessions/ea-php56" 
    php_value upload_max_filesize 128M 
    php_flag zlib.output_compression Off 
</IfModule> 
# END cPanel-generated php ini directives, do not edit 
<IfModule mod_rewrite.c> 
    RewriteEngine on 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)$ index.php/$1 [L,QSA] 
</IfModule>