CodeIgniter Mod_Rewrite问题

时间:2012-03-03 22:59:35

标签: .htaccess codeigniter

删除index.php时遇到问题?来自网址:

http://localhost/index.php?/reset_password

使用htaccess文件:

# Customized error messages.
ErrorDocument 404 /index.php
# Set the default handler.
DirectoryIndex index.php
# Various rewrite rules.
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>

CI设置:

$config['uri_protocol'] = 'AUTO';

我在这里查看了其他线程,遇到了类似的问题但是他们的htaccess文件都没有工作。我错过了一个设置吗?

2 个答案:

答案 0 :(得分:5)

我之前从未见过.htaccess设置,虽然它可能有用。我已经将这个用于我所完成的每个CI项目,试一试:

<IfModule mod_rewrite.c>

    RewriteEngine on

    # If the start of the URL doesn't match one of these...
    RewriteCond $1 !^(index\.php|robots\.txt|assets|cache|themes|uploads)

    # Route through index.php
    RewriteRule ^(.*)$ index.php/$1 [L]

</IfModule>

添加请勿忘记config.php

/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = '';

请记住:这不会自动从您的网址中“删除”index.php,而是在请求通过它时将其排除(如果不存在)。如果您有包含index.php的硬编码链接,则必须更改它们,否则您需要额外的.htaccess配置。如果您使用的是CI网址功能,例如base_url()site_url()anchor(),如果您在上面的配置中将其显示为空白,则不会使用index.php

答案 1 :(得分:0)

此代码适用于我:

如果我的网站名称为 codeIgniter_ci_intro

我在.htaccess中使用了代码

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /codeIgniter_ci_intro//index.php/$1 [L]

我在config.php中更改了

$config['index_page'] = '';