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

有四中步骤可以实现PHP的伪静态,你造吗

Jun 13, 2016 pm 12:28 PM
apache index nbsp php test

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

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

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

 

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

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


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

<span style="color: #000000;">RewriteEngine OnRewriteRule </span>^/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'来举例

<span style="color: #0000ff;">echo</span> <span style="color: #800080;">$_SERVER</span>['PATH_INFO']; <span style="color: #008000;">//</span><span style="color: #008000;">输出'/c/index/a/test/id/100'</span>
Copy after login

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

 

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

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

ErrorDocument 404 /404page.php
Copy after login

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

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

 

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

<span style="color: #000000;">RewriteEngine OnRewriteCond </span>%{REQUEST_FILENAME} !-<span style="color: #000000;">fRewriteCond </span>%{REQUEST_FILENAME} !-<span style="color: #000000;">dRewriteRule </span>. index.php
Copy after login

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

<span style="color: #008000;">/*</span><span style="color: #008000;">** 获取当前请求的URI地址[email protected] void[email protected] painsOnline[email protected] string URI</span><span style="color: #008000;">*/</span><span style="color: #0000ff;">function</span><span style="color: #000000;"> getReqUri(){    </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">trim</span>(<span style="color: #800080;">$_SERVER</span>["REQUEST_URI"<span style="color: #000000;">]);}</span><span style="color: #800080;">$reqUri</span> =<span style="color: #000000;"> getReqUri();</span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">preg_match</span>('/^\/test.html/isU', <span style="color: #800080;">$reqUri</span><span style="color: #000000;">)){</span><span style="color: #008000;">//</span><span style="color: #008000;">解析请求地址</span>    <span style="color: #0000ff;">include</span> 'test.php'<span style="color: #000000;">;    </span><span style="color: #0000ff;">exit</span><span style="color: #000000;">();}</span>
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
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

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.

NGINX and Apache: Understanding the Key Differences NGINX and Apache: Understanding the Key Differences Apr 26, 2025 am 12:01 AM

NGINX and Apache each have their own advantages and disadvantages, and the choice should be based on specific needs. 1.NGINX is suitable for high concurrency scenarios because of its asynchronous non-blocking architecture. 2. Apache is suitable for low-concurrency scenarios that require complex configurations, because of its modular design.

See all articles