【问题标题】:Remove index.php from url in CodeIgniter 3从 CodeIgniter 3 中的 url 中删除 index.php
【发布时间】:2016-11-23 11:33:00
【问题描述】:

我在 CodeIgniter 3 中做一个项目。我需要从 url 中删除 index.php。为此,请帮助我获取 CodeIgniter 3 的 .htaccess 文件以及该文件的放置位置。

这是我的基本网址

http://cableandmedia.com/demo/

【问题讨论】:

标签: php .htaccess codeigniter codeigniter-3


【解决方案1】:

使用以下代码更新您的 htaccess 文件

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

在配置文件中,请使用以下代码更改基本网址:-

$root  = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url']    = $root;

【讨论】:

    【解决方案2】:

    将您的 .htacces 文件放在根目录并使用以下代码:

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

    【讨论】:

      【解决方案3】:
      1. 在项目的root处创建.htaccess文件

      .htaccess

      <IfModule mod_rewrite.c>
        RewriteEngine On
        # !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
        #  slashes.
        # If your page resides at
        #  http://www.example.com/mypage/test1
        # then use
        # RewriteBase /mypage/test1/
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^(.*)$ index.php?/$1 [L]
      </IfModule>
      
      <IfModule !mod_rewrite.c>
        # If we don't have mod_rewrite installed, all 404's
        # can be sent to index.php, and everything works as normal.
        # Submitted by: ElliotHaughin
          ErrorDocument 404 /index.php
      </IfModule>
      
      <IfModule mod_headers.c>
          Header set Access-Control-Allow-Origin: "*"
      </IfModule>
      

      2。删除root/application/config/config.php处的index.php

      $config['index_page'] = '';
      

      3。如果您的服务器是新的 |您的服务器重写模式被拒绝,而不是 Open Terminal Connect Your Server Via SSH Type below Code

      sudo a2enmod rewrite
      sudo service apache2 restart
      sudo nano /etc/apache2/apache2.conf
      

      之后请检查AllowOverride是否为All

      <Directory “/var/www”>
        AllowOverride All
      </Directory>
      

      【讨论】:

        【解决方案4】:

        文件:config.php

        $config['index_page'] = '';
        

        文件:.htaccess

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

        RewriteEngine 允许您重写进入服务器的 URL 请求,并且基于正则表达式解析器。

        【讨论】:

          【解决方案5】:

          在您的config.php 中进行以下更改。

          $config['index_page'] = '';
          

          并在主项目目录中添加.htaccess文件,内容如下

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

          【讨论】:

            【解决方案6】:

            嗯,

            我做了什么,

            我为此编辑/创建了“.htaccess”

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

            和“app/config/config.php”

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

            和“app/libraries/Template.php”

            define("PATH", base_url()."index.php/");
            define("BASE", base_url());
            

            干杯!

            【讨论】:

              猜你喜欢
              • 2015-09-28
              • 2021-12-15
              • 2022-01-06
              • 2020-08-12
              • 2016-06-25
              • 2016-05-14
              • 2014-10-28
              • 2012-06-15
              • 2012-09-21
              相关资源
              最近更新 更多