Redirect user via 301 - too many loops?
Posted: Thu Oct 08, 2015 11:19 am
I am trying to write a dynamic script that will perform a 301 if a product ID either doesn't exist, or has been moved to a new Category or Sub Category.
The code below should first see if the product ID exists.
If it doesn't, then it will take the user to the SubID in the URL.
If that doesn't exist, it will go to the CatID.
If that doesn't exist either, it will finally go to a nice "page not found".
But if the Product ID DOES exist, but the CatID or SubID are incorrect (as in, they are not in the Product's row in the database... (product might have been moved to a new category), I want them to be 301d to the new URL.
Problem is, when I load a product page and alter the SubID to test it, it just loops and loops and never stops. Leaving a white page.
The code below should first see if the product ID exists.
If it doesn't, then it will take the user to the SubID in the URL.
If that doesn't exist, it will go to the CatID.
If that doesn't exist either, it will finally go to a nice "page not found".
But if the Product ID DOES exist, but the CatID or SubID are incorrect (as in, they are not in the Product's row in the database... (product might have been moved to a new category), I want them to be 301d to the new URL.
Problem is, when I load a product page and alter the SubID to test it, it just loops and loops and never stops. Leaving a white page.
Code: Select all
function checkURL()
{
$pageURL = (isset($_SERVER['HTTPS']) && ($_SERVER["HTTPS"] == "on")) ? 'https://' : 'http://';
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
$url = checkURL();
$fullurl = "http://rs.site.co.uk"."$url";
$str = explode("/","$fullurl");
if (!isset($_SESSION["loggedin"]))
{
if (isset($id))
{
$urlfind = $str[count($str)-2];
$urlsubname = $str[count($str)-3];
$urlsubid = $str[count($str)-4];
$urlcatname = $str[count($str)-5];
$urlcatid = $str[count($str)-6];
$query = "SELECT id FROM products WHERE id = :id";
$result = $pdo->prepare($query);
$result->execute(array(':id' => $id));
$num_rows = $result->rowCount();
// If the product ID is not found, it will revert to the Sub or Cat
if ($num_rows == 0)
{
$queryredir = ("SELECT DISTINCT subid FROM products WHERE subid =:urlsubid");
$resultredir = $pdo->prepare($queryredir);
$resultredir->execute(array(':urlsubid' => $urlsubid));
$num_rowssub = $resultredir->rowCount();
if ($num_rowssub == "1")
{
header("Location: /subcateg/".urlencode($urlcatid)."/".urlencode($urlcatname)."/".urlencode($urlsubid)."/". urlencode($urlsubname)."/",TRUE,301);
}
if ($num_rowssub == "0")
{
$queryredir = ("SELECT DISTINCT catid FROM products WHERE catid =:urlcatid");
$resultredir = $pdo->prepare($queryredir);
$resultredir->execute(array(':urlcatid' => $urlcatid));
$num_rowscat = $resultredir->rowCount();
if ($num_rowscat == "1")
{
header("Location: /categ/".urlencode($urlcatid)."/".urlencode($urlcatname)."/",TRUE,301);
}
if ($num_rowscat == 0)
{
header("HTTP/1.1 404 Not Found", true, 404);
include ("custom_404.php");
exit();
}
}
}
// If the product ID *IS* found... is the rest of the URL correct?
if ($num_rows <> 0)
{
$query2 = "SELECT COUNT(id) AS numrows FROM products WHERE (catid <> :c OR subid <>:s) AND id =:id";
$result2 = $pdo->prepare($query2);
$result2->execute(array(':c' => $_GET['c'],':s' => $_GET['s'],':id' => $id));
$num_rows3 = $result2->rowCount();
if ($num_rows3 == "1")
{
// gather the information for the new URL
$query3 = ("SELECT catid, subid, catname, subname, id, title FROM products WHERE id =:id GROUP BY id");
$result3 = $pdo->prepare($query3);
$result3->execute(array(':id' => $id));
while ($row = $result3->fetch(PDO::FETCH_OBJ))
{
$title = "$row->title";
$findtitle ="/ /";
$replacetitle ="-";
$titlereplace = preg_replace ($findtitle, $replacetitle, $title);
$categ = "$row->catname";
$findcateg ="/ /";
$replacecateg ="-";
$categreplace = preg_replace ($findcateg, $replacecateg, $categ);
$subcateg = "$row->subname";
$findsubcateg ="/ /";
$replacesubcateg ="-";
$subcategreplace = preg_replace ($findsubcateg, $replacesubcateg, $subcateg);
// 301 to new product page URL
header("Location: /product/".urlencode($row->catid)."/".urlencode($categreplace)."/".urlencode($row->subid)."/".urlencode($subcategreplace)."/".urlencode($row->id)."/".urlencode($titlereplace)."/",TRUE,301);
}
}
}
}
}
?>