如何在CodeIgniter的路径中去除 "index.php"

116

我该如何移除CodeIgniter中每个路径中突出显示的"index.php"在某些地方位于中心位置? 我想要干净的没有index.php的URL?


https://www.formget.com/codeigniter-htaccess-remove-index-php/ - Eldho NewAge
27个回答

90
如果您正在使用Apache,请在您的根网络目录中放置一个.htaccess文件,其中包含以下内容:
RewriteEngine on
RewriteCond $1 !^(index\.php|[Javascript / CSS / Image root Folder name(s)]|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

这里有另一个很好的版本:

http://snipplr.com/view/5966/codeigniter-htaccess/


2
+1 的链接!虽然需要稍微调整一下。用以下代码可成功 --> RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [QSA,L] - OrangeRind
由于某些原因,我不得不在RewriteCond的开头添加一个斜杠。 RewriteCond $1 !^\/(index\.php|assets|robots\.txt) - Henry
由于某些原因,如果您在URL中有index.php,它实际上不会被删除,它会保留并继续工作。 - CMCDragonkai
1
我尝试了所有可能的组合,但它仍然无法正常工作!!!你是如何让它正常工作的?? - Roshdy
您的Apache安装需要配置以检查.htaccess文件 - 确保已经这样设置。 - Sean Vieira

61
我在删除index.php时遇到了一些大问题。一般来说,下面的.htaccess文件已经在多个服务器上进行了测试,通常是有效的:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]  

<Files "index.php">
AcceptPathInfo On
</Files>  

如果你对此没有什么运气,那么下一步就是调整你的配置文件。尝试一些其他的URI协议,例如。
| '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

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

如果你还是没有运气,试试将重写规则更改为包括你的子文件夹。如果你在开发服务器上使用临时URL,这通常是一个问题。
RewriteRule ^(.*)$ /subofolder1/subfolder2/index.php/$1 [L]  

只是随便尝试一下这些选项,其中一个应该会起作用。另外,请确保您的索引文件已设置为:
$config['index_page'] = '';

我曾以为你的第一段代码是解决我的方法,但事实上它只是将类似于 domain/* 的任何网址发送到 domain/index.php ,并没有像发送 domain/welcome 到 welcome 控制器。当时我很兴奋,但现在正在尝试其他建议。 - jsky

32

将.htaccess文件与index.php文件放置在应用程序根目录中。(检查htaccess扩展名是否正确,因为htaccess.txt对我不起作用。)

然后在.htaccess文件中添加以下规则:

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

然后在你的application/config/config.php文件中找到以下行:

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

将变量empty设置如下。

$config['index_page'] = '';

就是这样,对我起作用了。

如果还不行,请尝试逐个替换以下变量('AUTO', 'PATH_INFO', 'QUERY_STRING', 'REQUEST_URI'和'ORIG_PATH_INFO')的参数。

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

3
谢谢,我一直以来都不知道为什么会失败,但是你的方法像黄油一样运作良好 :D - Ahmed Gaber
1
感谢MDeSilva。这篇帖子节省了我的时间。 - sanji
1
谢谢。它有效。顺便说一下,我不需要更改config.php。 - Stony
1
谢谢,这个对我很有帮助,我在WAMP中使用了PATH_INFO。 - T.Coutlakis

21

请查看 system\application\config\config.php 文件,其中有一个名为 index_page 的变量。

它应该是这样的:

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

将其更改为

$config['index_page'] = "";

如前所述,您还需要向.htaccess文件添加重写规则。

注意:在CodeIgniter v2中,此文件已从system文件夹移动到application\config\config.php中。


9

我尝试了以上所有方法,但都失败了。后来我发现,在我的虚拟主机文件/etc/apache2/sites-available/default中,我没有将AllowOverride None更改为AllowOverride All

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory />
            Options FollowSymLinks
            AllowOverride All    <---- replace None with All
    </Directory>
    <Directory /var/www >
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All   <---  replace None with All
            Order allow,deny
            allow from all
    </Directory>

     ...


非常感谢,我也将AllowOverride None设置好了,现在可以工作了! - Willy
这个博客现在已经消失了,我非常想读它。但是这些解决方案对我都不起作用... - Kevin Beal
太棒了,救了我的一天。 - JChow

6

步骤1 => 将您的.htaccess文件放置在应用程序和系统文件夹存在的根目录中。

步骤2 => 如果您的Web路径使用子文件夹,例如-yourdomain.com/project/,则在htaccess文件中使用以下代码

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

如果您的网址路径使用根文件夹,比如 - yourdomain.com - 那么请在htaccess文件中使用以下代码。
RewriteEngine on
RewriteBase    /
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

尝试使用上述示例中的第一个仅适用于我的根目录,但不适用于表单加载等,我仍将尝试在URL中使用index.php,如果我能弄清楚,我会继续尝试。 - Someone
感谢@Vinod。我因这个问题而疯狂了。我在网上搜索了很长时间,尝试了各种解决方案,但它们从未奏效,直到我找到了你的解决方案。现在我的CI项目正在运行,并给了我继续使用CI的新希望。非常感谢 :) - gomesh munda

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

在将 RewriteRule .* index.php/$0 [PT,L] 更改为项目文件夹名称“codeigniter”之后,它对我起作用了。感谢大家的支持。

5

我想添加一些内容

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA] 

请确保编辑您的应用程序/config.php变量,将.htaccess文件替换为以下方式:

替换

$config['uri_protocol'] = “AUTO” 

使用

$config['uri_protocol'] = “REQUEST_URI”

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

这一定会起作用的...试一下吧


4

完美解决方案 [已在本地主机和真实域上测试通过]

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接