Table of Contents
前言
PHP 伪静态
使用apache实现伪静态
第一步:httpd.conf去注释
第二步:更改重写权限
第三步:按需添加伪静态规则
重写规则详解
Summary
Home Backend Development PHP Tutorial Detailed introduction to PHP pseudo-static graphic and text code

Detailed introduction to PHP pseudo-static graphic and text code

Mar 20, 2017 pm 04:05 PM

前言

关于伪静态的话题,众说纷纭。我不是很在意这些讨论,但是有一些大牛给出的看法确实是很有味道的, 而且也是比较的公正。

使用了伪静态的话,会耗费CPU资源,但是对于SEO什么的更加有益;
 不适用伪静态而是使用纯静态(如html)的话,会造成硬盘读写瓶颈;

不管怎样,秉承学习新知识的理念,了解一下伪静态的实现还是挺好的。与JavaWeb中实现伪静态的思路有点不太一致(但是原理其实上是一致的啦,不过多解释了)。PHP中实现微静态稍稍的复杂了一点点,但是却更加的灵活了,维护起来也很方便。

PHP 伪静态

关于PHP实现伪静态的方法不仅可以在路由方面解决,还可以借助apache,ngnix服务器等方式来实现。今天刚好遇到了这么话题,在此做下记录,以备不时之需。

使用apache实现伪静态

借助于Apache服务器的方式来实现我个人有一点点的理解。类似于做饭。

我们做饭之前是需要有一套厨具的, 然后需要点火,再根据做什么菜来选择不同的烹饪方式。

同样的,本方式原理类似。首先Apache服务器就是这样的一套厨具,然后我们开启“伪静态方法”就好比点了个火。最后根据不同的重写规则来完成不同的伪静态实现。

这样一来就很容易理解了吧。

第一步:httpd.conf去注释

在apache服务器的安装目录下找到httpd.conf文件,然后打开找到下图所示的mode_rewrite.so行记录项,去掉该行最前面的那个#号注释符即可。
Detailed introduction to PHP pseudo-static graphic and text code

查看此项内容是否打开可以借助phpinfo()探针函数来实现。这点比较基础,在此就不作介绍了。

  • 开启之前:
    Detailed introduction to PHP pseudo-static graphic and text code

  • Detailed introduction to PHP pseudo-static graphic and text code:
    Detailed introduction to PHP pseudo-static graphic and text code

第二步:更改重写权限

下面正式“点火”吧。具体就是在刚才的httpd.conf文件下找到 AllowOverride None ,然后修改为AllowOverride All,如此即可。
Detailed introduction to PHP pseudo-static graphic and text code

第三步:按需添加伪静态规则

现在就是具体的重写规则了。具体的实现方式为在需要进行“伪静态”的项目的根目录下添加一个.htaccess 文件。

通常来说我们会使用这样的方式来实现PHP的伪静态化。

代码借鉴于网络

.htaccess 文件中 写入

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule index.html$ index.php    .
    RewriteRule index-([1-9]+[0-9]*).html$ index.php?p=$1
    RewriteRule ([a-z]{1,})-([0-9]{1,}).html$ index.php?action=$1&id=$2</IfModule>
Copy after login

具体的含义如下:

  • RewriteEngine : 重写引擎开关,On为打开, Off为关闭。

- RewriteRule: 重写规则,也就是我们的菜谱了,针对每一个规则将按照由上往下的优先级进行匹配,可以写多个重写规则。

重写规则详解

规则模板为: 关键字RewriteRule 静态路由 $ 动态PHP路由。切记之间有空格哈。

其实学过Python中一个叫Django的框架的都应该知道啦,其实就是个类似于匹配转换的过程,一对一转换。没学过的这样一说也应该能很轻松的理解啦。

关键在于静态路由可以使用正则表达式来进行匹配,这样正好照应了php动态参数的特点。

下面来实战一下吧。
准备一下代码,在项目目录中添加刚才的.htaccess文件,规则如上。然后添加一个index.php文件。代码内容如下:

<?php/**
 * Created by PhpStorm.
 * User: ${郭璞}
 * Date: 2017/1/18
 * Time: 11:07
 */if($_GET[&#39;name&#39;]) {
    echo "Name: ".$_GET[&#39;name&#39;]."<br />";
}if($_GET[&#39;age&#39;]) {
    echo "Age: ".$_GET[&#39;age&#39;]."<br />";
}
Copy after login

入门级: RewriteRule index.html$ index.php

严格匹配,在浏览器中输入参数为index.html的时候,apache就会把客户端的请求发往index.php来进行解析。
Detailed introduction to PHP pseudo-static graphic and text code

Detailed introduction to PHP pseudo-static graphic and text code

Rookie level: RewriteRule index-([a-z0-9]*).html$ index.php?name=$1

Add the regular matching method below.
Use PHP dynamic method!
Detailed introduction to PHP pseudo-static graphic and text code

Use pseudo-static method (due to Chinese encoding issues, English letters are used instead)
Detailed introduction to PHP pseudo-static graphic and text code

But if there is a matching error, it will report 404 error.
Detailed introduction to PHP pseudo-static graphic and text code

Advanced level: RewriteRule ([a-z]{1,})-([0-9]{1,}).html$ index.php?name=$1 &age=$2

With the above foreshadowing, the following is not difficult to understand. The rules are arbitrary (lowercase letters and numbers. Routes in the form of .html will be placed on the subsequent action parameters and id parameters)
Detailed introduction to PHP pseudo-static graphic and text code

Summary

Well, roughly So much. In fact, there are many techniques for implementing pseudo-static methods. Here is a brief introduction to make a record, which can be regarded as a starting point.

And the .htaccess files in many projects are really classic. We should learn from it more.

Finally, today is the first day of the new year in 2017. I wish you who read this article

Happy New Year and freebug programming in the new year.

The above is the detailed content of Detailed introduction to PHP pseudo-static graphic and text code. For more information, please follow other related articles on the PHP Chinese website!

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)

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

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

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

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

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

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.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles