【PHP】伪静态 - 1. 使用正则表达式实现

在我们实际开发中,有需要,不希望使用真静态,但是希望利于SEO, 可以考虑使用伪静态。

http://localhost/news.php?type=music&id=100

我们希望这个地址可以用下面的访问url来替换

http://localhost/new-music-id=100.html

 

上面的问题可以使用伪静态:

1. 实现方式有, 直接使用正则表达式。

2. 使用apahce自带的rewrite机制来完成。

 

看一个需求:

http://localhost/news.php/1,23,456.html, 问这个页面是否给已被访问到?

Answer: 答案是可以被访问到,可以查看$_SERVER变量中的PATH_INFO

现在希望上面的地址换成下面的地址: http://localhost/news.php?a=1&b=122&c=219

<?php

    //http://localhost/bigwebsite/staticPage/fa/demo.php/1,23,456.html
    //通过$_SERVER['PATH_INFO'],可以得到1,23,456.html
    $path_info = $_SERVER['PATH_INFO'];
    //观察1,23,456.html,写出正则表达式
    $reg = '/(\d+),(\d+),(\d+)\.html$/i';
    
    preg_match($reg, $path_info, $res);
    
    echo "<pre>";
    print_r($res);
    echo "</pre>";
    /*
    
        Array
        (
            [0] => 1,23,456.html
            [1] => 1
            [2] => 23
            [3] => 456
        )
    
    */
    
    //取出所有捕获的子表达式,然后进行url的拼接即可
    //http://localhost/news.php?a=$res[1]&b=$res[2]&c=$res[3]
?>

 

当然有更好的做法,这种做法了解即可。

posted @ 2013-06-03 04:16  Zhentiw  阅读(251)  评论(0编辑  收藏  举报