【问题标题】:Codeigniter | Remove index.php from site_url or anchor functions代码点火器 |从 site_url 或锚函数中删除 index.php
【发布时间】:2015-03-28 23:53:59
【问题描述】:

我需要从site_url()返回值中删除index.php 我不希望搜索引擎缓存我的 URL 包含 index.php。

我通过设置 .htaccess 文件从 url 中删除了 index.php,但仍然无法正常工作。

例如,当我在项目的一部分中使用 site_url() 时,搜索引擎会缓存 URL,例如 http://url/index.php/controller。我从 system/helper 中的 site_url 函数中删除了 index.php,但我认为 redirect 和 form_open 函数不能正常工作。

【问题讨论】:

    标签: php codeigniter url uri config


    【解决方案1】:

    写入您的 .htaccess 文件

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

    并在 config.php 中设置 index_page 配置

    $config['index_page'] = '';
    

    【讨论】:

      【解决方案2】:

      你可以使用base_url()函数:

      echo base_url(); // http://example.com/website
      echo site_url(); // http://example.com/website/index.php
      

      【讨论】:

      • base_url() 也将返回 http://example.com/website/index.php,如果您在 config.php 中设置了 $config['index_page'] = 'index.php'
      【解决方案3】:

      CodeIgniter 4

      提供的答案似乎是指旧版本的 CodeIgniter,现在配置略有不同

      1. 转到/app/Config/App.php
      2. 找到public $indexPage属性并将其更改为:

      public $indexPage = '';
      
      1. 转到/public/.htaccess
      2. 确保这段代码或多或少如下,默认情况下应该已经这样设置了

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

      使用 .env 进行配置

      您也可以使用.env 配置文件

      1. 转到.env
      2. 将此添加到文件中

      app.indexPage = ''
      

      然后您可以使用不带index.php 作为尾随文件的方法

      site_url()
      echo anchor('news/local/123', 'My News', 'title="News title"');
      

      文档

      【讨论】:

        猜你喜欢
        • 2014-01-15
        • 2013-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-25
        • 1970-01-01
        • 2019-03-22
        • 1970-01-01
        相关资源
        最近更新 更多