【问题标题】:How can i avoid index.php from URL in codelgniter [closed]如何避免 index.php 从 codeigniter 中的 URL [关闭]
【发布时间】:2012-03-16 09:06:09
【问题描述】:

我正在使用 apache 服务器,我需要从 uri 中删除 index.php

【问题讨论】:

  • 请。谁对这样的问题投赞成票?它是否显示了任何研究成果?

标签: php codeigniter


【解决方案1】:

就在 CI 用户指南中(删除 index.php 文件):https://www.codeigniter.com/user_guide/general/urls.html

【讨论】:

    【解决方案2】:

    如果您没有 .htaccess 文件,请创建它并在其中添加此代码:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]
    
    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]
    
    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    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> 
    

    然后在 application/config/config.php 中找到 $config['index_page'] = 'index.php';并替换为 $config['index_page'] = ''; 附言Sarfraz 提供的答案仅适用于本地机器。我提供的可以在全球范围内使用

    【讨论】:

    • 与官方用户指南中的建议相比,这很复杂。
    【解决方案3】:

    打开 config.php 并替换

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

    在.htaccess文件中添加

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

    在某些情况下,uri_protocol 的默认设置无法正常工作。要解决这个问题,只需在 config.php 中替换

    $config['uri_protocol'] = "AUTO" by $config['uri_protocol'] = "REQUEST_URI"
    

    【讨论】:

      猜你喜欢
      • 2011-02-15
      • 2012-08-10
      • 2015-10-24
      • 2012-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-18
      相关资源
      最近更新 更多