- parse_str: documentation ( source)
- parse_url: documentation ( source)
<?php
$links = array(
"product.php?shopprodid=1057&variantid=2942" => "https://storksak.com/changing-bags",
"index.php?pageid=2" => "https://storksak.com/changing-bags/olivia",
"changing-bags/shoulder-bag/emma.html" => "https://storksak.com/changing-bags/emma"
);
foreach ($links as $url => $tourl){
$parsed_url = parse_url($url);
$query = isset($parsed_url['query']) ? $parsed_url['query'] : '';
parse_str($query,$parts);
if (isarray($parts)){
if (isset($parts["shopprodid"]) && isset($parts["variantid"])){
echo 'RewriteCond %{QUERY_STRING} ^shopprodid='.$parts["shopprodid"].'&variantid='.$parts["variantid"].'$'."\n";
echo 'RewriteRule ^product.php$ '.$tourl.' [R=301,L]'."\n";
} elseif (isset($parts["pageid"])){
echo 'RewriteCond %{QUERY_STRING} ^pageid='.$parts["pageid"].'$'."\n";
echo 'RewriteRule ^index.php$ '.$tourl.' [R=301,L]'."\n";
} elseif (isset($parts["shopcatid"])){
echo 'RewriteCond %{QUERY_STRING} ^shopcatid='.$parts["shopcatid"].'$'."\n";
echo 'RewriteRule ^shop.php$ '.$tourl.' [R=301,L]'."\n";
}
} else {
echo 'RewriteRule ^'.$url.'$ '.$tourl.' [R=301,L]'."\n";
}
}