<?php// localhost/php100/test.php?id|1@action|2$Php2Html_FileUrl = $_SERVER["REQUEST_URI"];echo $Php2Html_FileUrl . "
"; // /php100/test.php?id|1@action|2$Php2Html_UrlString = str_replace("?", "", str_replace("/", "", strrchr(strrchr($Php2Html_FileUrl, "/") , "?")));echo $Php2Html_UrlString . "
"; // id|1@action|2$Php2Html_UrlQueryStrList = explode("@", $Php2Html_UrlString);print_r($Php2Html_UrlQueryStrList); // Array ( [0] => id|1 [1] => action|2 )echo "
";foreach ($Php2Html_UrlQueryStrList as $Php2Html_UrlQueryStr) { $Php2Html_TmpArray = explode("|", $Php2Html_UrlQueryStr); print_r($Php2Html_TmpArray); // Array ( [0] => id [1] => 1 ) ; Array ( [0] => action [1] => 2 ) echo "
"; $_GET[$Php2Html_TmpArray[0]] = $Php2Html_TmpArray[1];}//echo '假静态:$_GET变量
';print_r($_GET); // Array ( [id|1@action|2] => [id] => 1 [action] => 2 )echo "
";echo "
";echo $_GET[id] . "
"; // 1echo $_GET[action]; // 2?>
php程序实现伪静态url,伪静态方法二:
<?php// localhost/php100/test.php/1/2$filename = basename($_SERVER['SCRIPT_NAME']);echo $_SERVER['SCRIPT_NAME'] . "
"; // /php100/test.phpecho $filename . "
"; // test.phpif (strtolower($filename) == 'test.php') { if (!emptyempty($_GET[id])) { $id = intval($_GET[id]); echo $id . "
"; $action = intval($_GET[action]); echo $action . "
"; } else { $nav = $_SERVER['REQUEST_URI']; echo "1:" . $nav . "
"; // /php100/test.php/1/2 $script = $_SERVER['SCRIPT_NAME']; echo "2:" . $script . "
"; // /php100/test.php $nav = ereg_replace("^$script", "", urldecode($nav)); echo $nav . "
"; // /1/2 $vars = explode("/", $nav); print_r($vars); // Array ( [0] => [1] => 1 [2] => 2 ) echo "
"; $id = intval($vars[1]); $action = intval($vars[2]); } echo $id . '&' . $action;}?>
php程序实现伪静态url,伪静态方法三:
<?phpfunction mod_rewrite() { global $_GET; $nav = $_SERVER["REQUEST_URI"]; echo $nav . "
"; $script_name = $_SERVER["SCRIPT_NAME"]; echo $script_name . "
"; $nav = substr(ereg_replace("^$script_name", "", urldecode($nav)) , 1); echo $nav . "
"; $nav = preg_replace("/^.ht(m){1}(l){0,1}$/", "", $nav); //这句是去掉尾部的.html或.htm echo $nav . "
"; $vars = explode("/", $nav); print_r($vars); echo "
"; for ($i = 0; $i < Count($vars); $i+= 2) { $_GET["$vars[$i]"] = $vars[$i + 1]; } return $_GET;}mod_rewrite();$year = $_GET["year"]; //结果为'2006'echo $year . "
";$action = $_GET["action"]; //结果为'_add'echo $action;?>
php程序实现伪静态url,伪静态方法四:
如果你有服务器权限我觉得还是使用apache伪静态.
A、Apache配置:
进入/etc/httpd/conf/目录下,打开httpd.conf文件,启用rewrite:
# LoadModule rewrite_module modules/mod_rewrite.so 去除前面的 #
启用.htaccess
AllowOverride None 修改为:AllowOverride All
B、Rewrite写法:
服务器有配置文件不可能由我们来改,所以大多情况下要在网站的根目录下建一个.htaccess文件,代码如下:
RewriteEngine on //启动rewrite引擎 RewriteRule ^/index([0-9]*).html$ /index.php?id=$1 //"([0-9]*)" 代表范围 用(.*)代表所有,下同。 RewriteRule ^/index([0-9]*)/$ /index.php?id=$1 [R] //虚拟目录
C、mod_rewrite 规则修正符
1) R 强制外部重定向
2) F 禁用URL,返回403HTTP状态码。
3) G 强制URL为GONE,返回410HTTP状态码。
4) P 强制使用代理转发。
5) L 表明当前规则是最后一条规则,停止分析以后规则的重写。
6) N 重新从第一条规则开始运行重写过程。
7) C 与下一条规则关联
如果规则匹配则正常处理,以下修正符无效
8) T=MIME-type(force MIME type) 强制MIME类型
9) NS 只用于不是内部子请求
10) NC 不区分大小写
11) QSA 追加请求字符串
12) NE 不在输出转义特殊字符 %3d$1 等价于 =$1
文章网址:
随意转载^^但请附上教学地址。