Home > Backend Development > PHP Tutorial > php实现伪静态

php实现伪静态

WBOY
Release: 2016-06-20 13:05:08
Original
1253 people have browsed it

伪静态主要是为了SEO而存在的一种技术,又名URL重写。至于为什么要实现伪静态这里就不详细解释了。一般实现伪静态技术都是使用apache服务的mod_rewrite功能,通过在名为.htaccess的文件里面书写一些rewrite规则的正则来实现URL重写技术,这种技术本篇不做详解,下面以程序为例主要讲一下利用PHP程序实现伪静态的方法。下面就是实现伪静态的php函数

 

</p>

<p><!--?php</p-->/*<br />
功能:PHP伪静态化页面的实现<br />
*/<br />
function mod_rewrite(){<br />
global $_GET;<br />
$nav=$_SERVER[&#39;REQUEST_URI&#39;];<br />
$script_name=$_SERVER[&#39;SCRIPT_NAME&#39;];<br />
$nav=substr(ereg_replace("^$script_name",&#39;&#39;,urldecode($nav)),1);<br />
$nav=preg_replace(&#39;/.htm(l){0,1}$/i&#39;,&#39;&#39;,$nav);//这句是去掉尾部的.html或.htm<br />
$vars=strpos($nav,&#39;/&#39;)!==false?explode(&#39;/&#39;,$nav):array();<br />
$nav=count($vars);<br />
if($nav){<br />
for($i=0;$i<$nav;$i+=2){<br />
$_GET[$vars[$i]]=$vars[$i+1];<br />
}<br />
}<br />
return $_GET;</p>

<p>}
Copy after login

例如对于如下这样的链接地址:

http://www.scutephp.com/index.php/controller/user/action/login.html

经过函数处理后,我们就可以得到他的链接原型了:

http://www.scutephp.com/index.php?controller=user&action=login

该函数的具体用法如下:

</p>

<p><!--?php</p-->mod_rewrite();<br />
$yearn=$_GET["controller"];//结果为&#39;user&#39;<br />
$action=$_GET["action"];//结果为&#39;login&#39;<br />
<br />
echo &#39;</p>

<pre class="brush:php;toolbar:false">
&#39;;
Copy after login

print_r($_GET);

 

至于具体情况,读者可根据自己的情况来吧,这里就不多说了。


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