【问题标题】:Disable index.php at url in CodeIgniter在 CodeIgniter 的 url 中禁用 index.php
【发布时间】:2013-10-12 12:00:42
【问题描述】:

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

CodeIgniter 使用index.php 为它的所有页面提供服务。因此,我们的 url 应该如下所示:

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

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

更改配置后
$config['index_page'] = 'index.php';

$config['index_page'] = '';

我可以在没有 index.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 的情况下可用。我不希望该站点与链接中的 index.php 文件一起运行。我希望http://www.example.com/codeigniter/something 完美运行,而http://www.example.com/codeigniter/index.php/something 不运行。我该怎么做?

【问题讨论】:

  • 你能粘贴你的虚拟主机配置吗?
  • 对不起,什么是虚拟主机配置?我正在使用 xampp 在 localhost 中托管我的项目

标签: php .htaccess codeigniter url-routing


【解决方案1】:

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

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

基本上,这条规则告诉如果请求包含codeigniter/index.php,则返回一个禁止的响应

【讨论】:

    【解决方案2】:

    试试这个

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

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

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        【解决方案4】:

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

        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]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-11-03
          • 1970-01-01
          • 2014-07-20
          • 1970-01-01
          • 1970-01-01
          • 2014-07-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多