Home > Article > Backend Development > PHP simple route analysis, you can set the suffix at will (html, do, shtml, aspx, etc.)_PHP tutorial
[php]
if(isset($_SERVER['PATH_INFO'])){
$str=substr($_SERVER['PATH_INFO'],0,strpos($_SERVER['PATH_INFO'],'.'));
//Get path information (pathinfo)
$pathinfo=explode('/',trim($str,'/'));
//Get $_GET['m']
$_GET['m']=(!emptyempty($pathinfo[0])?$pathinfo[0]:'index');
//Remove the beginning of the array unit
array_shift($pathinfo);
//Get $_GET['a]
$_GET['a']=(!emptyempty($pathinfo[0])?$pathinfo[0]:'index');
//Remove the beginning of the array unit
array_shift($pathinfo);
$num=count($pathinfo);
for ($i=0;$i<$num;$i+=2){
$_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']); //Remove m from the array
$a=$_GET['a'];
unset($_GET['a']); //Remove a from the array
$query=http_build_query($_GET); // Form new URL parameters
//Combine a new URL
$url=$_SERVER['SCRIPT_NAME']."/{$m}/{$a}/".str_replace(array("&","="),"/",$query).".html" ; //The point is here
header("Location:".$url);
}
}
$str=substr($_SERVER['PATH_INFO'],0,strpos($_SERVER['PATH_INFO'],'.'));
//Get path information (pathinfo)
$pathinfo=explode('/',trim($str,'/'));
//Get $_GET['m']
$_GET['m']=(!empty($pathinfo[0])?$pathinfo[0]:'index');
//Remove the beginning of the array unit
Array_shift($pathinfo);
//Get $_GET['a]
$_GET['a']=(!empty($pathinfo[0])?$pathinfo[0]:'index');
//Remove the beginning of the array unit
Array_shift($pathinfo);
$num=count($pathinfo);
for ($i=0;$i<$num;$i+=2){
$_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']); //Remove m
from the array
$a=$_GET['a'];
Unset($_GET['a']); //Remove a
from the array
$query=http_build_query($_GET); //Create new URL parameters
//Combine new URL
$url=$_SERVER['SCRIPT_NAME']."/{$m}/{$a}/".str_replace(array("&","="),"/",$query).".html" ; //The point is here
header("Location:".$url);
}
}