search
HomeBackend DevelopmentPHP TutorialCI框架 .htaccess 隐藏url中index.php的解决

CodeIgniter(以下简称"CI")是一款国外优秀的PHP轻量级MVC框架,它支持PHP4和PHP5,是开发中小型可拓展性需求高的Web应用程序的利器。目前你所见到的这个博客程序,正是采用CI进行的编写。

秉承MVC架构的思想,CI中的所有控制器都需要经过单点入口文件index.php(默认)来加载调用。也就是说,在默认情况下,所有CI开发项目的URL都形如以下这种形式:

http://localhost/index.php/blog/logs/this_is_a_test_entry

很显然,默认情况下,index.php在URL地址段中的存在一定程度上影响了URL的简洁和SEO的进行。我们可以通过下面本文介绍的方法来去掉这个讨厌的Index.php。

你或许已经注意到在CodeIgniter用户手册中,已经存在关于此问题的解决方法。但官方提供的这个.htaccess配置,并不是所有时候都能解决问题。本文现在给出一个更完善的解决方案。

注意:在继续之前,请确认你的主机支持.htaccess配置。其中,如果Apache作为Web服务器,需要开启mod_rewrite模块的支持;如果将IIS作为Web服务器,则需要额外安装ISAPI_Rewrite拓展。

具体方法如下:

1. 将以下配置信息复制并保存为.htaccess文件。

以下为.htaccess文件信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14

RewriteEngineOn

RewriteBase /  

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d  

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

#如果没有安装mod_rewrite模块,所有的404页面都将被 #发送到index.php,此时,程序会像没有设置隐藏时一样运行   ErrorDocument404 /index.php

 2. 将以上.htaccess文件上传到CI所在项目的根目录(即与index.php同级目录下)

3. 修改application/config.php中的如下参数:

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

1 $config['index_page'] = ""; //设置为空

以上三步,缺一不可。如果一切配置正常,你会发现,再次运行程序的时候,程序已经自动隐藏index.php这个URL段了!

Trackback(UTF-8):http://www.cnSaturn.com/trackback/40



CodeIgniter中开启PATH_INFO时mod_rewrite隐藏index.php的问题

在CodeIgniter中,当我将URI寻址方式从AUTO更改为PATH_INFO时,即:

$config['uri_protocol'] = 'PATH_INFO';

注:PATH_INFO的开启,是因为我希望通过$_GET来取值,而不是系统默认的POST方式。

在此情况下如何仍然使用以上.htaccess方案,结果将是:index.php顺利隐藏,但主控制器并不能正确的获取值。

解决方案如下,就一步:

去掉以下重写规则中index.php后面的问号即可。

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

修改后的规则如下:

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

其他地方不变。


=============================================================================================================


【其他】跟我学网站开发框架CodeIgniter之url篇

如何删除index.php文件

估计很多人学习CodeIgniter第一步想做的就是如何去掉index.php,这个官方手册就有相关教程,修改.htaccess 文件(前提是你的服务器是apache):

RewriteEngine on

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

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

当然了,有很多人按照要求修改了,但是却出现了错误,所有的访问都404了,而且,这个404是apache的404页面,而不是CodeIgniter的404错误页面。

出现这种问题,是对apache的rewrite规则不理解:

  • 第一行、将RewriteEngine引擎设置为on,就是让url重写生效;
  • 第二行、配置url重写规则,!^(index\.php|images|robots\.txt) 这个正则表达式指明了哪些文件不需要重写,而是直接访问;
  • 第三行、^(.*)$是一个正则表达式,意思是对所有请求都发送到/index.php/$1,熟悉url的人都知道,以反斜杠(/)开头的,都是相对路径,相对于谁呢?根,也就是网址。
  • 所以呢,如果CodeIgniter如果不是安装在网站的根目录,必然会出现错误。如何解决呢,在CodeIgniter手册中也给出了相应的解决方案:

    把上面最后一句改为:RewriteRule ^(.*)$ index.php/$1 [L]

    只需要去掉index.php前面的斜杠就行。

    如何添加url后缀

    通过上面的步骤,我们已经隐藏了index.php了,现在我们制作的网站更加的rest了,一般人已经无法一眼就看出你的网站是用CodeIgniter开发的,还是ROR开发的。

    但是,如何在url后面增加后缀呢,这样,我们甚至可以隐藏或者伪造网站的开发语言,通过修改 config/config.php 文件,你可以为 CodeIgniter 生成的 URL 添加一个指定的文件后缀,比如你可以添加.html,甚至你可以添加.asp,.jsp。

    这样我们就可以将 http://www.hualai.net.cn/index.php/news/view/about 变成 http://www.hualai.net.cn/index.php/news/view/about.html。

    如何使用查询字符串

    一般情况下我们不需要使用查询字符串,但是,总有一些特殊情况,是我们用CodeIgniter的rest模式无法完成的,这样我们就需要在 URL 中使用查询字符串:

    index.php?c=products&m=view&id=345

    CodeIgniter 默认此功能是关闭的,如果想开启的话,打开配置文件 application/config/config.php 您可以看到如下内容:

    $config['enable_query_strings'] = FALSE;

    $config['controller_trigger'] = 'c'; //控制器名

    $config['function_trigger'] = 'm'; //方法名

    $config['directory_trigger']='d'; //控制器所在子目录名称

    如果你将 enable_query_strings 更改为 TRUE ,那么这个功能就被激活了。此时,你就可以通过关键字来调用需要的控制器和方法了:

    index.php?c=controller&m=method

    当我们在使用CodeIgniter制作分页的时候,这个就可以派上用场了。


    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Explain the concept of a PHP session in simple terms.Explain the concept of a PHP session in simple terms.Apr 26, 2025 am 12:09 AM

    PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

    How do you loop through all the values stored in a PHP session?How do you loop through all the values stored in a PHP session?Apr 26, 2025 am 12:06 AM

    In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

    Explain how to use sessions for user authentication.Explain how to use sessions for user authentication.Apr 26, 2025 am 12:04 AM

    The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

    Give an example of how to store a user's name in a PHP session.Give an example of how to store a user's name in a PHP session.Apr 26, 2025 am 12:03 AM

    Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

    What are some common problems that can cause PHP sessions to fail?What are some common problems that can cause PHP sessions to fail?Apr 25, 2025 am 12:16 AM

    Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

    How do you debug session-related issues in PHP?How do you debug session-related issues in PHP?Apr 25, 2025 am 12:12 AM

    Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

    What happens if session_start() is called multiple times?What happens if session_start() is called multiple times?Apr 25, 2025 am 12:06 AM

    Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

    How do you configure the session lifetime in PHP?How do you configure the session lifetime in PHP?Apr 25, 2025 am 12:05 AM

    Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    DVWA

    DVWA

    Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools

    Atom editor mac version download

    Atom editor mac version download

    The most popular open source editor

    EditPlus Chinese cracked version

    EditPlus Chinese cracked version

    Small size, syntax highlighting, does not support code prompt function

    MinGW - Minimalist GNU for Windows

    MinGW - Minimalist GNU for Windows

    This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.