删除Codeigniter URL中的index.php

时间:2014-03-01 13:38:55

标签: php apache .htaccess codeigniter mod-rewrite

如何在Codeigniter网址中删除index.php?

http://localhost/Sample/index.php

http://localhost/Sample

我一整天都在网上搜索,没有任何作用。我正在使用Apache2.2并且已经启用了httpd.conf中的mod_rewrite。我使用的版本是Codeigniter 2.1.4。

请一步一步地教我。我仍然是Codeigniter Framework的新手。谢谢!

5 个答案:

答案 0 :(得分:2)

您可以在.htaccess文件中使用以下代码:

RewriteEngine On
RewriteBase /my_project_name/
# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]

答案 1 :(得分:1)

您必须在两个地方进行此更改,其中一个位于/application/config/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

第二个位置(如上所述)更新/创建.htaccess文件,该文件位于与主index.php文件相同的根文件夹中。

参考CI指南: http://ellislab.com/codeigniter/user-guide/general/urls.html

这一切都在指南中......

答案 2 :(得分:0)

在根文件夹中找到的.htaccess文件中尝试此操作:

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

答案 3 :(得分:0)

You just need to create .htaccess file in project folder and write:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

 # Rewrite all other URLs to index.php/URL 
 RewriteRule ^(.*)$ index.php?url=$1 [PT,L] 

 </IfModule>
  <IfModule !mod_rewrite.c>
       ErrorDocument 404 index.php
   </IfModule>

   #And You don't need to define in base_url in config file:

         $config['base_url'] = '';// blank it.

  I hope it will work perfectly ...

答案 4 :(得分:0)

首先在Apache服务器中启用“ mod_rewrite”。 重新启动您的Apache服务器。

将此代码添加到.htaccess(文件夹结构为Project_Name / .htaccess)

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