Home Backend Development PHP Tutorial 有四中方法可以实现PHP的伪静态,你造吗?

有四中方法可以实现PHP的伪静态,你造吗?

Jun 23, 2016 pm 01:19 PM

说起伪静态的实现方案,你是不是很爽快的回答"简单,配置下apache的重写规则就行了嘛"

但是你有没有发现这种情况,你最近弄了很多新功能,每天上几个新功能,每天都有好多伪静态配置,才开始两天运维同学还乐意配合,过两天运维同学就要骂了。你麻痹,脑残为什么不一次搞完,天天麻烦我。但是了,你要上线啊,不得不苦逼的求运维同学了,然后说出一句程序猿界最不要脸的话"这次最后一次改动了",然后后面又要改,哎,你的人格算是扫地了。。。

 

如果有这样的烦恼存在,请看下面的文章,保证你以后再也不求运维了,想干啥就干啥。。。

 那PHP实现伪静态有多少种方法了?个人见解和统计奥,有四种方法


1、使用apache的URL重写规则,这个大家都懂,在apache里面配置, 这里同学们都造,只列举一段简单的配置

RewriteEngine OnRewriteRule ^/test.html index.php?controller=index&action=test [L]
Copy after login

2、使用PHP的pathinfo , 你是不是有看到有的网站这样玩'www.xxx.com/index.php/c/index/a/test/id/100' , 当然要支持这种你需要把'php.ini' 中的参数

'cgi.fix_pathinfo' 设置为1。拿'www.xxx.com/index.php/c/index/a/test/id/100'来举例

echo $_SERVER['PATH_INFO']; //输出'/c/index/a/test/id/100'
Copy after login

到这,应该明白了吧,你再对这段进行解析,分配实际地址

3、使用404机制,一般情况下伪静态都是实际不存在的页面,因此可以使用apache 404配置,但是有些问题,就是'post'类型的请求会被抛弃,导致你无法获取'$_POST',

但是'$_GET'仍然可以获取, 假设此处404页面为'404page.php', apache 配置如下:

ErrorDocument 404 /404page.php
Copy after login

然后在 '404page.php'中嵌入如下代码

header("HTTP/1.1 200 OK"); //这里一定要有,不然状态就是404$reqUrl = $_SERVER['REQUEST_URI']; // 请求地址/*** 从URL中解析参数*/function parseUrlParams($queryUrl){    $arr = explode('?', $queryUrl);    parse_str($arr[1], $param);    if($param)    {        foreach($param as $key => $value)        {            $_GET[$key] = $value;        }    }}parseUrlParams($reqUrl); // url解析参数//然后你就可以使用 $reqUrl 根据自己的规则匹配不同的实际请求地址if(preg_match('#^/test.html#is', $reqUrl, $matches)){   include('index.php');   die();}
Copy after login

4、方法3的改进型,方法3在apache内部机制相当于重定向了,导致post(get)传递的参数无法获取。分析上面的其实是找不到相关文件,那当服务器找不到相关文件时,我们为它指定一个文件,不就OK了,它就不用跳转了,这时POST之类都不会丢失。apache 配置如下:

RewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . index.php
Copy after login

上面一段配置的大概意思是 当请求的文件或者目录无法找到时 使用根目录下的 'index.php' 替代,那这时你就可以在'index.php'中获取相关参数并解析到实际请求地址

/*** 获取当前请求的URI地址*@param void*@author painsOnline*@return string URI*/function getReqUri(){    return trim($_SERVER["REQUEST_URI"]);}$reqUri = getReqUri();if(preg_match('/^\/test.html/isU', $reqUri)){//解析请求地址    include 'test.php';    exit();}
Copy after login

 

鄙人才疏学浅,有不足之处,欢迎补足

 

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
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)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

See all articles