Table of Contents
ThinkPHP 利用.htaccess文件的 Rewrite 规则隐藏URL中的 index.php实现
去掉 URL 中的 index.php
Home php教程 php手册 (Nginx和PHP下)URL重写,TP实现URL重写

(Nginx和PHP下)URL重写,TP实现URL重写

Jun 06, 2016 pm 07:58 PM
nginx php thinkphp url accomplish rewrite

在thinkphp的案例中有一个.htaccess文件,里面配置了URL的一些重写规则,如: IfModule mod_rewrite.c RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] /IfModule

在thinkphp的案例中有一个.htaccess文件,里面配置了URL的一些重写规则,如:


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

它的作用就是设置URL重写以隐藏URL中含有的index.php。一般来说URL过长或者动态化的URL都不利于SEO因此隐藏的目的就是要达到更好的SEO效果。
明白了.htaccess文件的作用,接下来就是让它工作起来。
要使.htaccess文件起作用,通常需要服务器开启URL_REWRITE模块才能支持。

下面是Apache的配置过程:

1、httpd.conf配置文件中加载了mod_rewrite.so模块
    在httpd.conf配置文件搜索 LoadModule rewrite_module modules/mod_rewrite.so (Apache2是这个)去掉前面的#

2、AllowOverride None 将None改为 All
    在httpd.conf配置文件找到“AllowOverride None”将None改为 All。这点值得注意的是,“AllowOverride None”在文件中能找到几处,但要改的却只有一处。如图所示:

AllowOverride All

(Nginx和PHP下)URL重写,TP实现URL重写

3、确保项目(一般是前台项目)配置文件的URL_MODEL设置为2

4、把.htaccess文件放到入口文件的同级目录下

ThinkPHP 利用.htaccess文件的 Rewrite 规则隐藏URL中的 index.php实现

去掉 URL 中的 index.php

ThinkPHP 作为 PHP 框架,是单一入口的,那么其原始的 URL 便不是那么友好。但 ThinkPHP 提供了各种机制来定制需要的 URL 格式,配合 Apache .htaccess 文件,更是可以定制出人性化的更利于 SEO 的 URL 地址来。

.htaccess文件是 Apache 服务器中的一个配置文件,它负责相关目录下的网页配置。我们可以利用 .htaccess 文件的 Rewrite 规则来隐藏掉 ThinkPHP URL 中的 index.php 文件(即入口文件),这也是 ThinkPHP URL 伪静态的第一步。

例如原来的 URL 为:

http://127.0.0.1/index.php/Index/insert
Copy after login

去掉 index.php 之后变为:

http://127.0.0.1/Index/insert
Copy after login

如此一来,就变成了 http://服务器地址/应用模块名称/操作名称[/变量参数] 的常见 URL 格式。

更改 Apache httpd.conf 配置文件

提示:如果在虚拟主机商配置,请直接配置第三、四步,因为支持 .htaccess 的空间已经配置好了前面两步。

用编辑器打开 Apache 配置文件 httpd.conf(该文件位于 Apache 安装目录Apache2conf),并按如下步骤修改,。

一、加载了 mod_rewrite.so

确认加载了 mod_rewrite.so 模块(将如下配置前的 # 号去掉):

LoadModule rewrite_module modules/mod_rewrite.so
Copy after login

二、更改 AllowOverride 配置

更改需要读取 .htaccess 文件的目录,将原来的目录注释掉:

#<directory files group>
<directory e:>
</directory></directory>
Copy after login
更改 AllowOverride None 为 AllowOverride FileInfo Options ,更改后的配置如下所示:
#<directory files group>
<directory e:>
    AllowOverride FileInfo Options
</directory>
</directory>
Copy after login

.htaccess 是基于目录来控制的, 该句即表示需要读取 .htaccess 文件的目录,要根据实际具体 Apache 的解析目录来配置。虚拟主机如果提供 .htaccess 控制,一般都已经配置好了。

三、添加 .htaccess 文件 Rewrite 规则

在需要隐藏 index.php 的目录下(本教程中为 E:/html/myapp,也即入口文件所在目录)创建 .htaccess 文件,并写入如下规则代码:

<ifmodule mod_rewrite.c>
RewriteEngine on

#不显示index.php

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

</ifmodule>
Copy after login

如果网站已经有 .htaccess 文件,在里面添加该段配置规则即可。如果不能创建该文件(Windows 平台不能创建),可以从本站下载该文件,但该文件仅配置了隐藏 index.php 的规则,点击此处下载。

四、更改项目配置文件

编辑项目配置文件 Conf/config.php ,将 URL 模式配置为 2(Rewrite模式):

    'URL_MODEL'=>2,
Copy after login

至此,各个配置已经完成。保存各配置文件后,重启 Apache 服务器并删除 Runtime 目录下的项目缓存文件,在浏览器访问隐藏 index.php 后的地址测试是否成功:

http://127.0.0.1/html/myapp/Index/index
Copy after login

如果访问成功,那么利用 Apache .htaccess 文件的 Rewrite 规则隐藏 index.php 入口文件的配置就成功了。


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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

WordPress site file access is restricted: Why is my .txt file not accessible through domain name? WordPress site file access is restricted: Why is my .txt file not accessible through domain name? Apr 01, 2025 pm 03:00 PM

Wordpress site file access is restricted: troubleshooting the reason why .txt file cannot be accessed recently. Some users encountered a problem when configuring the mini program business domain name: �...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Top 10 PHP CMS Platforms For Developers in 2024 Top 10 PHP CMS Platforms For Developers in 2024 Dec 05, 2024 am 10:29 AM

CMS stands for Content Management System. It is a software application or platform that enables users to create, manage, and modify digital content without requiring advanced technical knowledge. CMS allows users to easily create and organize content

See all articles