Table of Contents
CodeIgniter中开启PATH_INFO时mod_rewrite隐藏index.php的问题
如何删除index.php文件
如何添加url后缀
如何使用查询字符串
Home Backend Development PHP Tutorial CI框架 .htaccess 隐藏url中index.php的解决

CI框架 .htaccess 隐藏url中index.php的解决

Jun 23, 2016 pm 01:57 PM
ci htaccess index.php solve hide

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 of this Website
    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

    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

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Solution to the problem that Win11 system cannot install Chinese language pack Solution to the problem that Win11 system cannot install Chinese language pack Mar 09, 2024 am 09:48 AM

    Solution to the problem that Win11 system cannot install Chinese language pack With the launch of Windows 11 system, many users began to upgrade their operating system to experience new functions and interfaces. However, some users found that they were unable to install the Chinese language pack after upgrading, which troubled their experience. In this article, we will discuss the reasons why Win11 system cannot install the Chinese language pack and provide some solutions to help users solve this problem. Cause Analysis First, let us analyze the inability of Win11 system to

    Five tips to teach you how to solve the problem of Black Shark phone not turning on! Five tips to teach you how to solve the problem of Black Shark phone not turning on! Mar 24, 2024 pm 12:27 PM

    As smartphone technology continues to develop, mobile phones play an increasingly important role in our daily lives. As a flagship phone focusing on gaming performance, the Black Shark phone is highly favored by players. However, sometimes we also face the situation that the Black Shark phone cannot be turned on. At this time, we need to take some measures to solve this problem. Next, let us share five tips to teach you how to solve the problem of Black Shark phone not turning on: Step 1: Check the battery power. First, make sure your Black Shark phone has enough power. It may be because the phone battery is exhausted

    The driver cannot be loaded on this device. How to solve it? (Personally tested and valid) The driver cannot be loaded on this device. How to solve it? (Personally tested and valid) Mar 14, 2024 pm 09:00 PM

    Everyone knows that if the computer cannot load the driver, the device may not work properly or interact with the computer correctly. So how do we solve the problem when a prompt box pops up on the computer that the driver cannot be loaded on this device? The editor below will teach you two ways to easily solve the problem. Unable to load the driver on this device Solution 1. Search for "Kernel Isolation" in the Start menu. 2. Turn off Memory Integrity, and it will prompt "Memory Integrity has been turned off. Your device may be vulnerable." Click behind to ignore it, and it will not affect the use. 3. The problem can be solved after restarting the machine.

    How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? Mar 22, 2024 am 08:06 AM

    With the continuous development of social media, Xiaohongshu has become a platform for more and more young people to share their lives and discover beautiful things. Many users are troubled by auto-save issues when posting images. So, how to solve this problem? 1. How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? 1. Clear the cache First, we can try to clear the cache data of Xiaohongshu. The steps are as follows: (1) Open Xiaohongshu and click the "My" button in the lower right corner; (2) On the personal center page, find "Settings" and click it; (3) Scroll down and find the "Clear Cache" option. Click OK. After clearing the cache, re-enter Xiaohongshu and try to post pictures to see if the automatic saving problem is solved. 2. Update the Xiaohongshu version to ensure that your Xiaohongshu

    Interpreting Oracle error 3114: causes and solutions Interpreting Oracle error 3114: causes and solutions Mar 08, 2024 pm 03:42 PM

    Title: Analysis of Oracle Error 3114: Causes and Solutions When using Oracle database, you often encounter various error codes, among which error 3114 is a relatively common one. This error generally involves database link problems, which may cause exceptions when accessing the database. This article will interpret Oracle error 3114, discuss its causes, and give specific methods to solve the error and related code examples. 1. Definition of error 3114 Oracle error 3114 pass

    How to hide WeChat friends without blocking or deleting them? How to hide WeChat friends without blocking or deleting them How to hide WeChat friends without blocking or deleting them? How to hide WeChat friends without blocking or deleting them Mar 13, 2024 pm 07:19 PM

    How to hide WeChat friends without blocking or deleting them? Many users want to hide some friends but don’t know how to do it. Let this site carefully introduce to users how to hide WeChat friends without blocking or deleting them. Methods to hide WeChat friends without blocking or deleting Method 1: 1. First open the WeChat software, find the address book on the WeChat page, and click "My". 2. Then we enter the settings page. 3. Find the “Privacy” option and click on it. 4. Then click "Don't let him see". 5. Go to the Do Not Let Her View page and click "+" to check the friends you want to hide.

    How to hide works in Douyin short videos How to hide personal video works How to hide works in Douyin short videos How to hide personal video works Mar 12, 2024 pm 12:49 PM

    There are many short video works provided in the Douyin short video app software. You can watch them as you like, and they are all permanently provided free of charge. Different types of live video channels are open, and all video content is original, with Give everyone the most satisfying way to watch. Enter your account to log in online, and a variety of exciting short videos will be pushed, which are accurately recommended based on what everyone watches every day. You can also enter the live broadcast room to interact and chat with the anchor, making you feel more happy. Works uploaded by individuals can also be hidden. It is very simple to set up with one click. You can see wherever you swipe. Swipe up and down to see the real-time comments of countless netizens. You can also share daily life dynamics. Now the editor has detailed online Douyin short videos. Users push for ways to hide personal video works. First open Douyin short video

    Are you worried about WordPress backend garbled code? Try these solutions Are you worried about WordPress backend garbled code? Try these solutions Mar 05, 2024 pm 09:27 PM

    Are you worried about WordPress backend garbled code? Try these solutions, specific code examples are required. With the widespread application of WordPress in website construction, many users may encounter the problem of garbled code in the WordPress backend. This kind of problem will cause the background management interface to display garbled characters, causing great trouble to users. This article will introduce some common solutions to help users solve the trouble of garbled characters in the WordPress backend. Modify the wp-config.php file and open wp-config.

    See all articles