CodeIgniter:无法从URL中删除“index.php”

时间:2013-08-07 17:39:59

标签: php codeigniter url url-rewriting

我一直在努力解决这个问题,并尝试了以下教程:

    stackiflow上的
  • This

  • 来自CI论坛的
  • This

还有很多其他人。

没有人工作过。

这是我的问题:

每当我点击WAMP localhost / ci / index.php / HomeController中的站点链接时 它工作正常。

如果我从网址中省略“index.php” (本地主机/ CI / HomeController中), 但是,它显示的是与localhost相同的文件夹列表。

这是root中的htaccess文件:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ci/

#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> 

在配置文件中:

$config['base_url'] = 'http://localhost/ci/';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO'; 

这仍然将我重定向到WAMP主页。

我也取消了这个(在httpd.conf文件中)。

LoadModule rewrite_module modules/mod_rewrite.so 

其他信息:

在我的routes.php文件中:

$route['default_controller'] = "HomeController";

我的缩写文件结构: (位于C:\ wamp \ www)

  • / CI
    • /应用
      • /高速缓存
      • /配置
      • /控制器
      • /芯
      • /错误
      • /助手
      • /钩
      • /语言
      • /库
      • /日志
      • /模型
      • / THIRD_PARTY
      • /视图
      • 的.htaccess
      • 的index.html
    • /资产
    • /系统
    • 的.htaccess
    • 的index.php

感谢任何帮助。对不起,如果已有关于此主题的答案。没有人为我的项目工作过。

修改

如果我尝试这个htaccess:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /ci/index.php?/$1 [L]

我明白了:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, admin@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

3 个答案:

答案 0 :(得分:2)

RewriteEngine on
RewriteBase /ci/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|js|img|css|captcha|robots\.txt)
RewriteRule ^(.*)$ /ci/index.php/$1 [L]

这是我用于删除index.php的项目的默认htaccess

RewriteBase/ci/)和RewriteRule/ci/index.php)应该引用项目的基本目录。也就是说,您的项目是否在http://www.yoursite.com/ci/托管?

另外,如果您在WAMP上托管项目,则需要在配置中启用htaccess。为此,单击WAMP图标并选择Apache-&gt; Apache Modules-&gt; rewrite_module。然后重启所有服务。

祝你好运!

答案 1 :(得分:0)

试试这个htaccess:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /ci/index.php?/$1 [L]

答案 2 :(得分:0)

在WAMP环境中,只需要三个步骤就可以从Codeigniter中的url中删除index.php。

1)与应用程序持有者并行创建.htacess文件,只需复制以下代码:

RewriteEngine On
RewriteBase /CodeIgniter/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

2)将$ config ['index_page']更改为应用程序文件夹中config.php中的空白,如下所示:

$config['index_page'] = '';

3)启用apache的“rewrite_module”。

重启你的apache并完成它。

详细信息:http://goo.gl/3jfW8f