如何转换这样的字符串:
[www.example.com?type=PC&brand=Dell&id=2]
到
[www.example.com/PC/Dell/2.html]
谢谢你的帮助!
如何转换这样的字符串:
[www.example.com?type=PC&brand=Dell&id=2]
到
[www.example.com/PC/Dell/2.html]
谢谢你的帮助!
如果您想将第一个链接映射到第二个链接,以使您网站上的 URL 更漂亮,请使用.htaccess
and Mod_rewrite
!
这是正确的解决方案:
$str = "[www.example.com?type=PC&brand=Dell&id=2]";
$str = trim($str,"[]"); //Remove square brackets, we'll add them back later
$url = parse_url($str); //Parse the URL into an array
$query = $url["query"]; //Part after the ?
$parts = explode("&", $query); //Have each GET variable into an array element
$parts = array_map(function($e) {
return preg_replace("/[^=]+=/", "", $e);
}, $parts); //Remove the part before the =.
$parts = implode("/", $parts); //Implode it back into a string.
$result = $url["path"] . "/" . $parts . ".html"; //Putting it back together
$result = "[$result]"; //I promised I'll put it back!
var_dump($result);
在你的 .htaccess 中试试这个
Options +FollowSymLinks
RewriteEngine on
RewriteRule /(.*)/(.*)/(.*)\.html www.example.com?type=$1&brand=$2&id=$3