Heim >Backend-Entwicklung >PHP-Tutorial >php 简单的路由解析 可以随意设置后缀(html,do,shtml,aspx等等)_PHP教程
[php]
if(isset($_SERVER['PATH_INFO'])){
$str=substr($_SERVER['PATH_INFO'],0,strpos($_SERVER['PATH_INFO'],'.'));
//获取路径信息(pathinfo)
$pathinfo=explode('/',trim($str,'/'));
//获取$_GET['m']
$_GET['m']=(!emptyempty($pathinfo[0])?$pathinfo[0]:'index');
//将数组单元的开头移除
array_shift($pathinfo);
//获取$_GET['a]
$_GET['a']=(!emptyempty($pathinfo[0])?$pathinfo[0]:'index');
//将数组单元的开头移除
array_shift($pathinfo);
$num=count($pathinfo);
for ($i=0;$i
$_GET[$pathinfo[$i]]=$pathinfo[$i+1];
}
}else{
$_GET['m']=(!emptyempty($_GET['m'])?$_GET['m']:'index');
$_GET['a']=(!emptyempty($_GET['a'])?$_GET['a']:'index');
if($_SERVER["QUERY_STRING"]){
$m=$_GET['m'];
unset($_GET['m']); //去除数组的m
$a=$_GET['a'];
unset($_GET['a']); //去除数组的a
$query=http_build_query($_GET); //组成新的URL参数
//组合新的URL
$url=$_SERVER['SCRIPT_NAME']."/{$m}/{$a}/".str_replace(array("&","="),"/",$query).".html"; //重点在这
header("Location:".$url);
}
}
if(isset($_SERVER['PATH_INFO'])){
$str=substr($_SERVER['PATH_INFO'],0,strpos($_SERVER['PATH_INFO'],'.'));
//获取路径信息(pathinfo)
$pathinfo=explode('/',trim($str,'/'));
//获取$_GET['m']
$_GET['m']=(!empty($pathinfo[0])?$pathinfo[0]:'index');
//将数组单元的开头移除
array_shift($pathinfo);
//获取$_GET['a]
$_GET['a']=(!empty($pathinfo[0])?$pathinfo[0]:'index');
//将数组单元的开头移除
array_shift($pathinfo);
$num=count($pathinfo);
for ($i=0;$i
$_GET[$pathinfo[$i]]=$pathinfo[$i+1];
}
}else{
$_GET['m']=(!empty($_GET['m'])?$_GET['m']:'index');
$_GET['a']=(!empty($_GET['a'])?$_GET['a']:'index');
if($_SERVER["QUERY_STRING"]){
$m=$_GET['m'];
unset($_GET['m']); //去除数组的m
$a=$_GET['a'];
unset($_GET['a']); //去除数组的a
$query=http_build_query($_GET); //组成新的URL参数
//组合新的URL
$url=$_SERVER['SCRIPT_NAME']."/{$m}/{$a}/".str_replace(array("&","="),"/",$query).".html"; //重点在这
header("Location:".$url);
}
}