PHP simulated URLRewrite

Source: Internet
Author: User
PHP simulates URLRewrite $ _ SERVER ['path _ info'] for more convenience. I checked some information about $ _ SERVER ['path _ info, modified the simulated URL Rewrite again.

The purpose is the same, but the class is not used. Instead, the obtained information is directly written to the predefined variables $ _ GET and $ _ SERVER ["QUERY_STRING, like yesterday's practice, $ _ SERVER ["R_QUERY_STRING"] is added to store the simulated QueryString.

And added a solution for the existence of/symbols in the URL. curly brackets are automatically added to indicate the difference. The test uses the $ _ GET value and generates Rewrite normally.

The content of the code is as follows:

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 <count ($ URLArray); $ 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, "&") {// when the URL is in the original state
$ ParaArray = explode ('&', $ _ SERVER ["QUERY_STRING"]);
For ($ I = 0; $ I <count ($ ParaArray); $ 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, "/") {// when the URL is a simulated URL Rewrite
$ QueryString = str_replace ('. html', '', $ QueryString );
$ _ SERVER ["R_QUERY_STRING"] = $ QueryString;
Preg_match_all ("/{(. *)}/U", $ QueryString, $ matches );
For ($ I = 0; $ I <count ($ matches [0]); $ I ++ ){
$ QueryString = str_replace ($ matches [0] [$ I], rawurlencode ($ matches [1] [$ I]), $ QueryString );
}
$ ParaArray = explode ('/', $ QueryString );
For ($ I = 0; $ I <count ($ ParaArray); $ I = $ I + 2 ){
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 ').'
';
?>

The URL of test 1 is http: // localhost: 808/test. php/a/123/B/456/d/789/{u/rl}/{http:/www.163.com/developer.html
Output result:

???????????????????? -
$ _ SERVER ["REQUEST_URI"] =/test. php/a/123/B/456/d/789/{u/rl}/{http:/www.163.com/developer.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/developer.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 /}

The URL of test 2 is http: // localhost: 808/test. php? A = 123 & B = 456 & c = & d = 789 & u/rl = http://www.163.com/
Output result:

???????????????????? -
$ _ 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/developer.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 /}

This is a success. we will continue testing and modifying the changes tomorrow. You are welcome to give your comments.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.