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

WBOY
Release: 2016-06-13 12:28:18
Original
1052 people have browsed it

有四中方法可以实现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

 

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

 

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!