无法从CodeIgniter中的url中删除index.php

时间:2014-01-05 19:19:26

标签: php .htaccess codeigniter mod-rewrite

我是CodeIgniter的新手,我正在试图找出为什么我在codeigniter网站和SO上看到的标准方法都不适用于我从网址中删除index.php在我的项目中。

我正在运行CentOs,我项目的路径如下: 的/ var / www / html等/ myproject的

我确保通过phpinfo()

检查httpd.conf中是否启用了mod_rewrite

我的.htacess文件如下所示:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /myproject/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

我的config.php看起来像这样(我也为uri_protocol尝试了PATH_INFO和REQUEST_URI):

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

我的routes.php看起来像这样:

$route['default_controller'] = "site";
$route['404_override'] = '';
$route['site/price/(.*)'] = "site/price/$1";

网站控制器的价格行为的签名是:

public function price($a, $b, $c) {
    //code
}

当我去

http://localhost/myproject/index.php/site/price/32137/2/1 

成功达到价格功能,但是当我尝试

http://localhost/myproject/site/price/32137/2/1 

我收到错误页面

The requested URL /myproject/site/price/32137/2/1 was not found on this server.

我错过了什么吗? 提前致谢

编辑: 如果有任何帮助,我可以使用url:localhost / myproject来访问我的项目的站点/索引方法,但其他任何操作都不可访问。

4 个答案:

答案 0 :(得分:0)

使用精确的.htaccess mod_rewrite配置提供in the docs for Codeigniter

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

每次都适合我。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

从网址中删除index.php有两个部分:

1)告诉.htaccess绕index.php路由。像这样:

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

2)编辑 system / cms / config / config.php 配置文件并从以下位置更新此设置:

$config['index_page'] = 'index.php';

$config['index_page'] = '';

这是我使用的示例.htaccess文件:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks -Indexes
    RewriteEngine On

    # Keep people out of codeigniter directory and Mercurial data
    RedirectMatch 403 ^/(/codeigniter|\.git|\.hg).*$

    #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} ^codeigniter.*
    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>
    ErrorDocument 404 public/index.php
</IfModule>

答案 3 :(得分:0)

这个怎么样

.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]