欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP模拟 URL Rewrite

程序员文章站 2024-01-20 09:01:46
...
$_SERVER['PATH_INFO']更方便,查看了一些关于 $_SERVER['PATH_INFO']的资料,对模拟 URL Rewrite 重新进行了修改。

目的是一样的,但并没有使用类了,而是直接将获取到的信息写入 $_GET 和 $_SERVER["QUERY_STRING"] 预定义变量,跟昨天的做法一样,新增 $_SERVER["R_QUERY_STRING"] 来存储模拟后的 QueryString。

并增加了对URL中存在 / 符号的解决办法,自动添加花括号以表示区别,测试用$_GET取值和生成Rewrite都很正常。

下面是代码的内容:


function BuildRewriteURL ( $string , $UrlStyle = 0 ) {
if ( strpos ( $string , ' ? ' )) {
$StrArray = explode ( ' ? ' , $string ) ;
$Page = $StrArray [ 0 ] ;
$ParaString = $StrArray [ 1 ] ;
}
else $ParaString = $string ;
$ParaString = str_replace ( ' ? ' , '' , $ParaString ) ;
if ( ! strpos ( $ParaString , ' & ' )) return $this -> ParaString ;
$URLArray = explode ( ' & ' , $ParaString ) ;
for ( $i = 0 ; $i $Para = explode ( ' = ' , $URLArray [ $i ]) ;
if ( ! empty ( $Para [ 1 ])) {
if ( ! empty ( $UrlString )) $UrlString .= ' / ' ;
if ( strpos ( $Para [ 0 ] , ' / ' )) $Para [ 0 ] = ' { ' . $Para [ 0 ] . ' } ' ;
if ( strpos ( $Para [ 1 ] , ' / ' )) $Para [ 1 ] = ' { ' . $Para [ 1 ] . ' } ' ;
$UrlString .= $Para [ 0 ] . ' / ' . $Para [ 1 ] ;
}
}
switch ( $UrlStyle ) {
case 1 : $AddString = ' / ' ; break ;
case 2 : $AddString = '' ; break ;
default : $AddString = ' .html ' ; break ;
}
return str_replace ( ' // ' , ' / ' , $Page . ' / ' . $UrlString . $AddString ) ;
}

$QueryString = ! empty ( $_SERVER [ ' PATH_INFO ' ]) ? $_SERVER [ ' PATH_INFO ' ] : str_replace ( $_SERVER [ " SCRIPT_NAME " ] , '' , $_SERVER [ " REQUEST_URI " ]) ;
$QueryString = str_replace ( array ( ' " ' , " ' " , ' ' ) , array ( ' %22 ' , ' %27 ' , ' %3C ' , ' %3E ' ) , $QueryString ) ;
if ( strrpos ( $QueryString , " & " ) && strrpos ( $QueryString , " & " )) { // 当URL为原始状态时
$ParaArray = explode ( ' & ' , $_SERVER [ " QUERY_STRING " ]) ;
for ( $i = 0 ; $i if ( strpos ( $ParaArray [ $i ] , ' = ' ) + 1 != strlen ( $ParaArray [ $i ])) {
$Para = explode ( ' = ' , $ParaArray [ $i ]) ;
if ( ! empty ( $Para [ 1 ])) {
if ( strpos ( $Para [ 0 ] , ' / ' )) $Para [ 0 ] = ' { ' . $Para [ 0 ] . ' } ' ;
if ( strpos ( $Para [ 1 ] , ' / ' )) $Para [ 1 ] = ' { ' . $Para [ 1 ] . ' } ' ;
$RequestURL .= ' / ' . $Para [ 0 ] . ' / ' . $Para [ 1 ] ;
}
}
}
$_SERVER [ " R_QUERY_STRING " ] = $RequestURL ;
}
elseif ( strrpos ( $QueryString , " / " )) { // 当URL为模拟 URL Rewrite 时
$QueryString = str_replace ( ' .html ' , '' , $QueryString ) ;
$_SERVER [ " R_QUERY_STRING " ] = $QueryString ;
preg_match_all ( " /{(.*)}/U " , $QueryString , $matches ) ;
for ( $i = 0 ; $i $QueryString = str_replace ( $matches [ 0 ][ $i ] , rawurlencode ( $matches [ 1 ][ $i ]) , $QueryString ) ;
}
$ParaArray = explode ( ' / ' , $QueryString ) ;
for ( $i = 0 ; $i if ( ! empty ( $ParaArray [ $i + 1 ])) {
if ( ! empty ( $RequestURL )) $RequestURL .= ' & ' ;
if ( strpos ( $ParaArray [ $i + 1 ] , ' %2F ' )) $ParaArray [ $i + 1 ] = rawurldecode ( $ParaArray [ $i + 1 ]) ;
if ( strpos ( $ParaArray [ $i + 2 ] , ' %2F ' )) $ParaArray [ $i + 2 ] = rawurldecode ( $ParaArray [ $i + 2 ]) ;
$StrArray [ $ParaArray [ $i + 1 ]] = $ParaArray [ $i + 2 ] ;
$RequestURL .= $ParaArray [ $i + 1 ] . " = " . $ParaArray [ $i + 2 ] ;
}
}
$_GET = $StrArray ;
$_SERVER [ ' QUERY_STRING ' ] = $RequestURL ;
}
unset ( $QueryString , $ParaArray , $Para , $StrArray , $RequestURL , $matches , $i ) ;

echo '


' ;
echo '
$_SERVER["REQUEST_URI"] = ' . $_SERVER [ ' REQUEST_URI ' ] ;
echo '
$_SERVER["QUERY_STRING"] = ' . $_SERVER [ ' QUERY_STRING ' ] ;
echo '
$_SERVER["R_QUERY_STRING"] = ' . $_SERVER [ ' R_QUERY_STRING ' ] ;
echo '
' ;
echo ' $_GET[ \' a \' ] = ' . $_GET [ ' a ' ] . '
' ;
echo ' $_GET[ \' b \' ] = ' . $_GET [ ' b ' ] . '
' ;
echo ' $_GET[ \' c \' ] = ' . $_GET [ ' c ' ] . '
' ;
echo ' $_GET[ \' d \' ] = ' . $_GET [ ' d ' ] . '
' ;
echo ' $_GET[ \' u/rl \' ] = ' . $_GET [ ' u/rl ' ] . '
' ;
echo '
' ;
echo ' BuildRewriteURL( \' /test.php?a=123&b=456&c=&d=789&u/rl=http://www.163.com/ \' ) = ' . BuildRewriteURL ( ' /test.php?a=123&b=456&c=&d=789&u/rl=http://www.163.com/ ' ) . '
' ;
echo ' BuildRewriteURL( \' /?a=123&b=456&c=&d=789&u/rl=http://www.163.com/ \' , \' 1 \' ) = ' . BuildRewriteURL ( ' /?a=123&b=456&c=&d=789&u/rl=http://www.163.com/ ' , ' 1 ' ) . '
' ;
echo ' BuildRewriteURL( \' a=123&b=456&c=&d=789&u/rl=http://www.163.com/ \' , \' 2 \' ) = ' . BuildRewriteURL ( ' a=123&b=456&c=&d=789&u/rl=http://www.163.com/ ' , ' 2 ' ) . '
' ;
?>

测试1的URL地址为:http://localhost:808/test.php/a/123/b/456/d/789/{u/rl}/{http:/www.163.com/}.html
输出结果:

????????????????????-
$_SERVER["REQUEST_URI"] = /test.php/a/123/b/456/d/789/{u/rl}/{http:/www.163.com/}.html
$_SERVER["QUERY_STRING"] = a=123&b=456&d=789&u/rl=http:/www.163.com/
$_SERVER["R_QUERY_STRING"] = /a/123/b/456/d/789/{u/rl}/{http:/www.163.com/}
????????????????????-
$_GET['a'] = 123
$_GET['b'] = 456
$_GET['c'] =
$_GET['d'] = 789
$_GET['u/rl'] = http:/www.163.com/
????????????????????-
BuildRewriteURL('/test.php?a=123&b=456&c=&d=789&u/rl=http://www.163.com/') = /test.php/a/123/b/456/d/789/{u/rl}/{http:/www.163.com/}.html
BuildRewriteURL('/?a=123&b=456&c=&d=789&u/rl=http://www.163.com/', '1') = /a/123/b/456/d/789/{u/rl}/{http:/www.163.com/}/
BuildRewriteURL('a=123&b=456&c=&d=789&u/rl=http://www.163.com/', '2') = /a/123/b/456/d/789/{u/rl}/{http:/www.163.com/}

测试2的URL地址为:http://localhost:808/test.php?a=123&b=456&c=&d=789&u/rl=http://www.163.com/
输出结果:

????????????????????-
$_SERVER["REQUEST_URI"] = /dcep2/test.php?a=123&b=456&c=&d=789&u/rl=http://www.163.com/
$_SERVER["QUERY_STRING"] = a=123&b=456&c=&d=789&u/rl=http://www.163.com/
$_SERVER["R_QUERY_STRING"] = /a/123/b/456/d/789/{u/rl}/{http://www.163.com/}
????????????????????-
$_GET['a'] = 123
$_GET['b'] = 456
$_GET['c'] =
$_GET['d'] = 789
$_GET['u/rl'] = http://www.163.com/
????????????????????-
BuildRewriteURL('/test.php?a=123&b=456&c=&d=789&u/rl=http://www.163.com/') = /test.php/a/123/b/456/d/789/{u/rl}/{http:/www.163.com/}.html
BuildRewriteURL('/?a=123&b=456&c=&d=789&u/rl=http://www.163.com/', '1') = /a/123/b/456/d/789/{u/rl}/{http:/www.163.com/}/
BuildRewriteURL('a=123&b=456&c=&d=789&u/rl=http://www.163.com/', '2') = /a/123/b/456/d/789/{u/rl}/{http:/www.163.com/}

大功告成,明天继续测试和修改,欢迎大家提出意见。