使用Codeigniter中的.htaccess文件简单地删除URI的index.php部分

使用Codeigniter中的.htaccess文件简单地删除URI的index.php部分,php,apache,.htaccess,codeigniter,mod-rewrite,Php,Apache,.htaccess,Codeigniter,Mod Rewrite,我正在尝试从我网站的URI中删除index.php部分。我对htaccess或apache一点也不精通,所以如果您的解决方案与此相关,请用我能理解的简单术语解释 目前,这只是一个学习项目,但我计划将此网站用作我家庭业务的销售场所,因此我必须弄清楚这一点,以获得更专业的外观 请注意:我已经阅读并尝试了CodeIgniter用户指南中给出的示例,但没有成功。也许这就是我放置.htaccess文件的地方 目前我正在尝试重写包含的文件,默认只有一行: Deny from all **同样,我已经尝试了

我正在尝试从我网站的URI中删除index.php部分。我对htaccess或apache一点也不精通,所以如果您的解决方案与此相关,请用我能理解的简单术语解释

目前,这只是一个学习项目,但我计划将此网站用作我家庭业务的销售场所,因此我必须弄清楚这一点,以获得更专业的外观

请注意:我已经阅读并尝试了CodeIgniter用户指南中给出的示例,但没有成功。也许这就是我放置.htaccess文件的地方

目前我正在尝试重写包含的文件,默认只有一行:

Deny from all
**同样,我已经尝试了CodeIgniter用户指南中的以下代码,但没有成功

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
我在下面包括主控制器、config.php文件、routes.php文件和我的网站基本导航视图。也许你会发现一个错误

主控制器

    <?php
session_start(); //we need to call PHP's session object to access it through CI
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Main extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    public function index() {
        $this->load->helper('url');
        $title = 'ImpactU Online';
        $subtitle = 'Is Your Next Web Project Ready?';
        $subhead = 'A Little About Us';
        $this->load->view('template/header', array(
            'title' => $title,
            'subtitle' => $subtitle,
            'subhead' => $subhead,
        ));
        $this->load->view('home');
        $this->load->view('template/footer');
    }

}

将htaccess文件更改为位于主文件夹中,然后重写为等于:

RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|js|css|uploads|favicon.png)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]

回答由核仁1985提供

如果使用xampp,我发现两个示例HTACE是最好的。我看你还没有把基本网址放进去。尽管codeigniter自动获取基本url,但最好将基本url也放在config.php中

注意:全部拒绝仅用于应用程序文件夹和系统文件夹htaccess文件。您很可能不需要触摸应用程序文件夹htaccss。如果需要从views文件夹中获取css和图像,只需在应用程序文件夹htaccess中说allow from all即可

要删除url中的index.php,请首先添加主目录htaccess。然后从config.php中删除index.php,但确保添加了基本url

Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
还是这个

<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>

重新启动发动机
重写基/
#删除用户对系统文件夹的访问权限。
#此外,这将允许您创建System.php控制器,
#以前这是不可能的。
#如果已重命名系统文件夹,则可以替换“系统”。
重写COND%{REQUEST_URI}^系统*
重写规则^(.*)$/index.php?/$1[L]
#当应用程序文件夹不在系统文件夹中时
#此代码段阻止用户访问应用程序文件夹
#提交人:Fabdrol
#将“应用程序”重命名为应用程序文件夹名称。
重写cond%{REQUEST_URI}^应用程序*
重写规则^(.*)$/index.php?/$1[L]
#检查用户是否试图访问有效文件,
#例如图像或css文档,如果这不是真的,它会发送
#请求index.php
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)$index.php?/$1[L]
#如果我们没有安装mod_rewrite,所有404
#可以发送到index.php,一切正常。
#提交人:ElliotHaughin
ErrorDocument 404/index.php

下面给出了htaccess文件的代码…用于从url中删除index.php

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ index.php?/$1 [L]

    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ index.php?/$1 [L]

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

    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]


</IfModule>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 index.php
</IfModule>

重新启动发动机
重写基/
重写COND%{REQUEST_URI}^系统*
重写规则^(.*)$index.php?/$1[L]
重写cond%{REQUEST_URI}^应用程序*
重写规则^(.*)$index.php?/$1[L]
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)$index.php?/$1[L]
RewriteCond%{THE_REQUEST}^GET.*index\.php[NC]
重写规则(.*)索引\.php/*(.*)/$1$2[R=301,NE,L]
ErrorDocument 404 index.php

下面是.htaccess的代码,用于从codeigniter中的URL中删除index.php

RewriteEngine on RewriteRule ^$ index.php [L] RewriteCond $1 !^(index\.php|private|public|templates|skin|css|scripts|images|robots\.txt|favicon\.ico) RewriteRule ^(.*)$ index.php/$1 [L] 重新启动发动机 重写规则^$index.php[L] 1美元^(索引\.php | private | public | templates | skin | css | scripts | images | robots\.txt | favicon\.ico) 重写规则^(.*)$index.php/$1[L] 此外,还需要将config.php中的index_页面设置为空
$config['index_page']=''

请核对我对这个问题的回答,如果你觉得有用的话,请尊重我。这是解决这个问题的一个很好的办法。没问题,能互相帮助很好。请注意
index\.php
后的htaccess,如果您的文件夹名为
a、b、c、d
,请将其更改为您的文件夹/文件名,并用
'.
分隔,这样您的
就可以重写$1^(index\.php | a | b | c | d等)
不幸的是,这不起作用。在控制器名找到index.php之前,我仍然需要键入index.php。否则,它会显示一个错误,表示找不到对象。您的服务器配置是否启用mod_rewrite?如果关闭,此ht访问将失败。我在本地使用wamp、xampp或EasyHP(有或没有virtualhost设置)时从来没有遇到过问题。添加一些解释我同意HaveNodeDisplayName,但我还是支持它,因为作为参考,它非常有用。-首先,您需要清空index.php$config['index_page']='';从config.php-然后您需要将此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>
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ index.php?/$1 [L]

    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ index.php?/$1 [L]

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

    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]


</IfModule>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 index.php
</IfModule>
RewriteEngine on RewriteRule ^$ index.php [L] RewriteCond $1 !^(index\.php|private|public|templates|skin|css|scripts|images|robots\.txt|favicon\.ico) RewriteRule ^(.*)$ index.php/$1 [L]