禁用 index.php 在 CodeIgniter 中的 url


Disable index.php at url in CodeIgniter

在使用codeigniter时,我遇到了一些困难。这是关于 URL 中的索引.php文件。

CodeIgniter 使用 index.php 提供所有页面。因此,我们的网址应如下所示:

http://www.example.com/codeigniter/index.php/something

我想通过编辑config.php文件并创建一个 .htaccess 文件来从 URL 中删除index.php。将配置从

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

$config['index_page'] = '';

我可以在没有索引的情况下运行我的项目.php就像这样:

http://www.example.com/codeigniter/something

所以现在,这个链接正在工作 - 但它也与index.php片段一起工作。我还可以看到带有http://www.example.com/codeigniter/index.php/something地址的组件。

我的.htaccess文件是:

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

现在,我想使我的网站仅在链接中没有index.php的情况下可用。我不希望这个网站在链接中使用索引.php文件运行。我希望http://www.example.com/codeigniter/something完美运行,http://www.example.com/codeigniter/index.php/something不运行。我该怎么做?

您可以尝试在.htaccess文件中添加以下内容

RewriteCond %{THE_REQUEST} codeigniter'/index'.php
RewriteRule codeigniter'/index'.php -[F]

基本上,如果请求包含代码点火器/索引,则此规则告诉返回禁止的响应.php

试试这个

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

将代码放在您的根文件夹 htaccess 中。如果您遇到任何问题,请告诉我

将此规则添加到您已有的规则之上:

RewriteCond %{THE_REQUEST} ' /codeigniter/index'.php([^'?' ]*)
RewriteRule ^ /codeignighter/%1 [L,R=301]

它会将包含index.php的请求重定向到没有它的 URL。

您应该在 index.php 之后添加一个 ?,以便它适用于所有主机。这是我的访问

RewriteEngine On
#RewriteBase /   maybe you don't need it, depends on the hosting
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index'.php|robots'.txt|humans'.txt|license'.txt|sitemap'.xml|assets)
RewriteRule ^(.*)$ index.php?/$1 [L]