Re: Redirect user via 301 - too many loops?
Posted: Tue Oct 13, 2015 10:40 am
What's the rewrite rule? If there's regex, you can easily make the trailing slash optional by adding a ?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
$stmt->execute(['id' => $id]);Code: Select all
<?php
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
$id = isset($_GET['product']) ? $_GET['product'] : null;
$h = isset($_GET['h']) ? $_GET['h'] : null;
$s = isset($_GET['s']) ? $_GET['s'] : null;
$c = isset($_GET['c']) ? $_GET['c'] : null;
$sname = isset($_GET['sname']) ? $_GET['sname'] : null;
$findsname ="/ /";
$replacesname ="-";
// rewriting subcategory name for use with hyphens in URL
$snamereplace = preg_replace ($findsname, $replacesname, $sname);
$cname = isset($_GET['cname']) ? $_GET['cname'] : null;
$findcname ="/ /";
$replacecname ="-";
// rewriting category name for use with hyphens in URL
$cnamereplace = preg_replace ($findcname, $replacecname, $cname);
$show = isset($_REQUEST['show']) ? $_REQUEST['show'] : null;
if (isset($show))
{
$rowsPerPage = $show;
}
else
{
if ( $detect->isMobile() && !$detect->isTablet() ) {
$rowsPerPage = 10;
}
else
{
$rowsPerPage = 44;
}
}
$pageNum = 1;
if(isset($_GET['pagenum']))
{
$pageNum = $_GET['pagenum'];
}
if (isset($c) && !isset($id) && !isset($s))
{
$query = "SELECT COUNT(id) AS numrows FROM products WHERE catid = :c AND pause = 'off'";
$result = $pdo->prepare($query);
$result->execute(array(':c' => $_GET['c']));
$numrows = $result->fetchColumn();
$maxPage = ceil($numrows/$rowsPerPage);
if ($pageNum > $maxPage)
{
// $c is the Category ID number in the URL
// $cnamereplace is from the variable at the top of this page - rewriting the category name from the database for the category
header("Location: /categ/".urlencode($c)."/".urlencode($cnamereplace)."/",TRUE,301);
}
}
if (isset($s) && !isset($id))
{
$query = "SELECT COUNT(id) AS numrows FROM products WHERE subid = :s AND pause = 'off'";
$result = $pdo->prepare($query);
$result->execute(array(':s' => $_GET['s']));
$numrows = $result->fetchColumn();
$maxPage = ceil($numrows/$rowsPerPage);
if ($pageNum > $maxPage)
{
// $c is the Category ID number in the URL
// $cnamereplace is from the variable at the top of this page - rewriting the category name from the database for the category
// $s is the SubCategory ID number in the URL
// $snamereplace is from the variable at the top of this page - rewriting the subcategory name from the database for the category
header("Location: /subcateg/".urlencode($c)."/".urlencode($cnamereplace)."/".urlencode($s)."/".urlencode($snamereplace)."/",TRUE,301);
}
}
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($c) && !isset($id) && !isset($s))
{
$urlcatname = $str[count($str)-1];
$queryredir = ("SELECT DISTINCT catid FROM products WHERE catid =:c");
$resultredir = $pdo->prepare($queryredir);
$resultredir->execute(array(':c' => $c));
$num_rows = $resultredir->rowCount();
if ($num_rows == 0)
{
header("HTTP/1.1 404 Not Found", true, 404);
include ("custom_404.php");
exit();
}
}
if (isset($s) && !isset($id))
{
function slugify($string, $find = ' ', $replace = '-')
{
return str_replace($find, $replace, $string);
}
$urlsubname = $str[count($str)-2];
$urlsubid = $str[count($str)-3];
$urlcatname = $str[count($str)-4];
$urlcatid = $str[count($str)-5];
$queryredir = ("SELECT catid, catname, subid, subname FROM products WHERE catid =:c AND catname =:catname AND subid =:s AND subname =:subname GROUP BY subid");
$resultredir = $pdo->prepare($queryredir);
$resultredir->execute(array(':c' => $c,':catname' => $cnamehyphenreplace, ':s' => $s,':subname' => $snamehyphenreplace));
$num_rows = $resultredir->rowCount();
if ($num_rows == "0")
{
$queryredir = ("SELECT DISTINCT catid FROM products WHERE catid =:c");
$resultredir = $pdo->prepare($queryredir);
$resultredir->execute(array(':c' => $c));
$num_rows = $resultredir->rowCount();
if ($num_rows == "1")
{
// $c is the Category ID number in the URL
// $urlcatname extracts the category name from the URL
header("Location: /categ/".urlencode($c)."/".urlencode($urlcatname)."/",TRUE,301);
}
if ($num_rows == 0)
{
header("HTTP/1.1 404 Not Found", true, 404);
include ("custom_404.php");
exit();
}
}
else
{
$subcategoryresult = $resultredir->fetch(PDO::FETCH_OBJ);
$catname = slugify($subcategoryresult->catname);
$subcatname = slugify($subcategoryresult->subname);
if ($subcategoryresult->catid != $urlcatid || $subcategoryresult->subid != $urlsubid) {
header("Location: /subcateg/$subcategoryresult->catid/".urlencode($catname)."/$subcategoryresult->subid/".urlencode($subcatname)."/", TRUE, 301);
}
}
}
function slugify($string, $find = ' ', $replace = '-')
{
return str_replace($find, $replace, $string);
}
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];
// Let's start by getting product information
$query = "SELECT id, catid, subid, catname, subname, title FROM products WHERE id = :id";
$stmt = $pdo->prepare($query);
$stmt->execute(array(':id' => $id));
// If the product ID is not found, it will revert to the Sub or Cat
if ($stmt->rowCount() == 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?
// ID is unique. If rowCount isn't 0, then it's 1 and we've found the product
else
{
// Just use the results of our first query
$product = $stmt->fetch(PDO::FETCH_OBJ);
$title = slugify($product->title);
$catname = slugify($product->catname);
$subcatname = slugify($product->subname);
$cnamehyphenreplace = str_replace("-"," ",$cname);
$snamehyphenreplace = str_replace("-"," ",$sname);
$titlehyphenreplace = str_replace("-"," ",$h);
if ($product->catid != $urlcatid || $product->subid != $urlsubid || $product->catname != $cnamehyphenreplace || $product->subname != $snamehyphenreplace || $product->title != $titlehyphenreplace) {
header("Location: /product/$product->catid/$catname/$product->subid/$subname/$product->id/$title), TRUE, 301);
}
}
}
}
?>Code: Select all
<?php
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
$id = isset($_GET['product']) ? $_GET['product'] : null;
$h = isset($_GET['h']) ? $_GET['h'] : null;
$s = isset($_GET['s']) ? $_GET['s'] : null;
$c = isset($_GET['c']) ? $_GET['c'] : null;
$sname = isset($_GET['sname']) ? $_GET['sname'] : null;
$findsname ="/ /";
$replacesname ="-";
// rewriting subcategory name for use with hyphens in URL
$snamereplace = preg_replace ($findsname, $replacesname, $sname);
$cname = isset($_GET['cname']) ? $_GET['cname'] : null;
$findcname ="/ /";
$replacecname ="-";
// rewriting category name for use with hyphens in URL
$cnamereplace = preg_replace ($findcname, $replacecname, $cname);
$show = isset($_REQUEST['show']) ? $_REQUEST['show'] : null;
if (isset($show))
{
$rowsPerPage = $show;
}
else
{
if ( $detect->isMobile() && !$detect->isTablet() ) {
$rowsPerPage = 10;
}
else
{
$rowsPerPage = 44;
}
}
$pageNum = 1;
if(isset($_GET['pagenum']))
{
$pageNum = $_GET['pagenum'];
}
if (isset($c) && !isset($id) && !isset($s))
{
$query = "SELECT COUNT(id) AS numrows FROM products WHERE catid = :c AND pause = 'off'";
$result = $pdo->prepare($query);
$result->execute(array(':c' => $_GET['c']));
$numrows = $result->fetchColumn();
$maxPage = ceil($numrows/$rowsPerPage);
if ($pageNum > $maxPage)
{
// $c is the Category ID number in the URL
// $cnamereplace is from the variable at the top of this page - rewriting the category name from the database for the category
header("Location: /categ/".urlencode($c)."/".urlencode($cnamereplace)."/",TRUE,301);
}
}
if (isset($s) && !isset($id))
{
$query = "SELECT COUNT(id) AS numrows FROM products WHERE subid = :s AND pause = 'off'";
$result = $pdo->prepare($query);
$result->execute(array(':s' => $_GET['s']));
$numrows = $result->fetchColumn();
$maxPage = ceil($numrows/$rowsPerPage);
if ($pageNum > $maxPage)
{
// $c is the Category ID number in the URL
// $cnamereplace is from the variable at the top of this page - rewriting the category name from the database for the category
// $s is the SubCategory ID number in the URL
// $snamereplace is from the variable at the top of this page - rewriting the subcategory name from the database for the category
header("Location: /subcateg/".urlencode($c)."/".urlencode($cnamereplace)."/".urlencode($s)."/".urlencode($snamereplace)."/",TRUE,301);
}
}
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($c) && !isset($id) && !isset($s))
{
$urlcatname = $str[count($str)-1];
$queryredir = ("SELECT DISTINCT catid FROM products WHERE catid =:c");
$resultredir = $pdo->prepare($queryredir);
$resultredir->execute(array(':c' => $c));
$num_rows = $resultredir->rowCount();
if ($num_rows == 0)
{
header("HTTP/1.1 404 Not Found", true, 404);
include ("custom_404.php");
exit();
}
}
if (isset($s) && !isset($id))
{
function slugify($string, $find = ' ', $replace = '-')
{
return str_replace($find, $replace, $string);
}
$urlsubname = $str[count($str)-2];
$urlsubid = $str[count($str)-3];
$urlcatname = $str[count($str)-4];
$urlcatid = $str[count($str)-5];
$queryredir = ("SELECT catid, catname, subid, subname FROM products WHERE catid =:c AND catname =:catname AND subid =:s AND subname =:subname GROUP BY subid");
$resultredir = $pdo->prepare($queryredir);
$resultredir->execute(array(':c' => $c,':catname' => $cnamehyphenreplace, ':s' => $s,':subname' => $snamehyphenreplace));
$num_rows = $resultredir->rowCount();
if ($num_rows == "0")
{
$queryredir = ("SELECT DISTINCT catid FROM products WHERE catid =:c");
$resultredir = $pdo->prepare($queryredir);
$resultredir->execute(array(':c' => $c));
$num_rows = $resultredir->rowCount();
if ($num_rows == "1")
{
// $c is the Category ID number in the URL
// $urlcatname extracts the category name from the URL
header("Location: /categ/".urlencode($c)."/".urlencode($urlcatname)."/",TRUE,301);
}
if ($num_rows == 0)
{
header("HTTP/1.1 404 Not Found", true, 404);
include ("custom_404.php");
exit();
}
}
else
{
$subcategoryresult = $resultredir->fetch(PDO::FETCH_OBJ);
$catname = slugify($subcategoryresult->catname);
$subcatname = slugify($subcategoryresult->subname);
if ($subcategoryresult->catid != $urlcatid || $subcategoryresult->subid != $urlsubid) {
header("Location: /subcateg/$subcategoryresult->catid/".urlencode($catname)."/$subcategoryresult->subid/".urlencode($subcatname)."/", TRUE, 301);
}
}
}
function slugify($string, $find = ' ', $replace = '-')
{
return str_replace($find, $replace, $string);
}
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];
// Let's start by getting product information
$query = "SELECT id, catid, subid, catname, subname, title FROM products WHERE id = :id";
$stmt = $pdo->prepare($query);
$stmt->execute(array(':id' => $id));
// If the product ID is not found, it will revert to the Sub or Cat
if ($stmt->rowCount() == 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?
// ID is unique. If rowCount isn't 0, then it's 1 and we've found the product
else
{
// Just use the results of our first query
$product = $stmt->fetch(PDO::FETCH_OBJ);
$title = slugify($product->title);
$catname = slugify($product->catname);
$subcatname = slugify($product->subname);
$cnamehyphenreplace = str_replace("-"," ",$cname);
$snamehyphenreplace = str_replace("-"," ",$sname);
$titlehyphenreplace = str_replace("-"," ",$h);
if ($product->catid != $urlcatid || $product->subid != $urlsubid || $product->catname != $cnamehyphenreplace || $product->subname != $snamehyphenreplace || $product->title != $titlehyphenreplace) {
header("Location: /product/$product->catid/$catname/$product->subid/$subcatname/$product->id/$title", TRUE, 301);
}
}
}
}
?>