【问题标题】:codeigniter remove index.php form urlcodeigniter 删除 index.php 表单 url
【发布时间】:2017-01-31 04:00:06
【问题描述】:

我正在尝试从 Codeigniter 的 url 中删除“index.php”。我知道如何从 example.com/controller 这样的基本 url 中删除 index.php。

但我不知道如何删除子文件夹中的“index.php”,例如 example.com/folder/index.php/controller

因为我必须在 codeigniter 中进行不同的项目。所以我在子文件夹中上传了第二个项目。

.htaccess

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

#modify
RewriteEngine on

RewriteCond $1 !^(index\.php|resources|robots\.txt)

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?/$1 [QSA,L]

RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

上面的程序用于基本 url。所以我将在这个程序中改变它在子文件夹上工作。

【问题讨论】:

    标签: php .htaccess codeigniter


    【解决方案1】:

    将此代码写入您的 .htaccess 文件中

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /folder/index.php [L]
    </IfModule>
    
    # END WordPress
    

    示例:example.com/folder/index.php/contorller

    上面的代码不需要写index.php

    它正在工作..

    【讨论】:

      【解决方案2】:

      在 config.php $config['index_page'] = ''; 中更新此变量。删除index.php

      【讨论】:

      • 仅此一项不会从您的 URL 中删除 index.php,您需要使用某种形式的 mod_rewrite(例如 htaccess 文件)将其正确删除。
      【解决方案3】:
       `Step:-1  Open the file config.php located in application/config path.  Find and Replace the below code in config.php  file.
      

      // 找到下面的代码

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

      // 删除index.php

       $config['index_page'] = ""
      

      步骤:-2 转到您的 CodeIgniter 文件夹并创建 .htaccess 文件。

      路径:

        Your_website_folder/
        application/
        assets/
        system/
        user_guide/
        .htaccess <--------- this file
        index.php
        license.txt
      

      步骤:-3 在 .htaccess 文件中写入以下代码

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

      步骤:-4 在某些情况下,uri_protocol 的默认设置无法正常工作。要解决这个问题,只需打开位于 application/config 中的 config.php 文件,然后找到并替换代码为:

      // 找到下面的代码

        $config['uri_protocol'] = "AUTO"
      

      // 替换为

       $config['uri_protocol'] = "REQUEST_URI" `
      

      【讨论】:

        【解决方案4】:

        采取与 example.com 文件夹完全相同的步骤,从 example.com/folder 文件夹的基本 URL 中删除 index.php。也就是说,编辑/创建 htaccess,从配置文件中删除 index.php,等等。

        【讨论】:

          【解决方案5】:

          快速解决方案

          RewriteEngine On
          RewriteCond %{HTTP} off
          RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
          

          如果遇到问题

          您可能需要检查您的apache 中是否安装并启用了modrewrite

          启用 modrewrite

          sudo a2enmod rewrite
          
          

          还要确保

          sudo vi /etc/apache2/apache2.conf
          

          并允许符号链接

          <Directory /var/www/>
                  Options Indexes FollowSymLinks
                  AllowOverride None
                  Require all granted
          </Directory>
          

          重新启动apache2,您的.htaccess 应该可以正常工作了。

          阅读最后一步:https://www.inteligentcomp.com/2017/07/how-to-setup-and-configure-lamp-on-ubuntu-server.html

          【讨论】:

            猜你喜欢
            • 2016-05-14
            • 2014-10-28
            • 2012-06-15
            • 2012-09-21
            • 2011-10-12
            • 1970-01-01
            • 2017-07-16
            相关资源
            最近更新 更多