php runtime、http web中rewrite浅解和方案-php教程

资源魔 44 0
本文针对函数较量争论的 php runtime web 相干运用开发, 提供一个简略完成url rewrite的计划,正在引见计划以前,咱们先看看相干的几个概念: 伪动态页面,静态页面,rewrite.

伪动态

动态网页

比方xxx网站上放了一个abc.html文件,你想拜访它就间接输出xxx. com/abc.html。Web效劳器看到这样的地点就间接找到这个文件输入给客户端。

静态网页

如果你想做一个显示以后工夫的页面,那末就能够写个PHP文件,而后拜访xxx. com/abc.php。Web效劳器看到这样的地点,找到abc.php这个文件,会交给PHP执行后前往给客户端。而静态网页往往要输出参数,以是地点就变为xxx. com/abc.php?a=1&b=2。

搜寻引擎比拟烦这类带问号的静态网页,由于参数能够随意加,而前往内容却没有变,以是会对这类网页降权。于是有了mod_rewrite,它能够从新映照地点。

rewrite

比方以后这个页面的地点 http://www.xxx.com/post/20153311,Web效劳器收到申请后会从新映照为 www.xxx.com/post.php?id=20153311,而后再执行阿谁PHP顺序。(以上彀址均为假定)这样,正在外部没有扭转的状况下,对外出现进去的网址变为了不问号的象动态网页的网址同样。于是有人给起了个名字叫“伪动态”。其实也没甚么伪的,就是不问号的动态网址,让搜寻引擎难受点罢了。

函数较量争论 php runtime 简略完成 rewrite 的一种办法

先以简略的nginx 中的一个简略的 rewrite 为例:

location ~ ^/(\w+)$ {
    rewrite /index.php?sub=$1;
}
location ~ ^/post/(\w+)/(\d+)$ {
    rewrite /post.php?class=$1&id=$2;
}

php url rewrite 简略完成

<?php
function rewrite_urls($s) 
{
    $in = array(
      '|^/post/(\\w+)/(\\d+)$|',
      '|^/(\\w+)$|'
    );
    $out = array(
      '/post.php?class=$1&id=$2',
      '/index.php?sub=$1',
    
    );
    return preg_replace($in, $out, $s); 
}
$post_url = '/post/literatrue/34';
echo rewrite_urls($post_url) .PHP_EOL;
$index_url = '/admin';
echo rewrite_urls($index_url) .PHP_EOL;

执行输入后果:

/post.php?class=literatrue&id=34
/index.php?sub=admin

因而正在应用 php runtime的时分,依据收到申请的uri(假定是/post/literatrue/34), 执行 rewrite_urls 函数(rewrite 规定填写正在这个函数的 $in 以及 $out 中), 而后将 rewrite 后的 uri (/post.php?class=literatrue&id=34) 作为挪用 fcPhpCgiProxy.requestPhpCgi 函数时,传入参数 $fastCgiParams 的一对 key-value

以上就是php runtime、http web中rewrite浅解以及计划的具体内容,更多请存眷资源魔其它相干文章!

标签: php开发教程 php开发资料 php开发自学 runtime rewrite

抱歉,评论功能暂时关闭!