Redirect user via 301 - too many loops?
Moderator: General Moderators
Re: Redirect user via 301 - too many loops?
What's the rewrite rule? If there's regex, you can easily make the trailing slash optional by adding a ?
Re: Redirect user via 301 - too many loops?
You can also test out your rewrite rules here: http://htaccess.madewithlove.be/
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: Redirect user via 301 - too many loops?
If I load this code into our live "beta" server, it kills the site.
Is it because of that "exit"???
It doesn't do that in my local version.
Is it because of that "exit"???
It doesn't do that in my local version.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Redirect user via 301 - too many loops?
No. Calling exit after header() is standard practice. "Kills the site" isn't enough info. Turn on error reporting to see what's actually happening.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: Redirect user via 301 - too many loops?
Code: Select all
$stmt->execute(['id' => $id]);Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Redirect user via 301 - too many loops?
Old version of PHP? Replace [] with array()
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: Redirect user via 301 - too many loops?
Yep - just did that like this:
$stmt->execute(array(':id' => $id));
Old version eh? mmm.
$stmt->execute(array(':id' => $id));
Old version eh? mmm.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: Redirect user via 301 - too many loops?
Bingo! Wow that was a good one.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Redirect user via 301 - too many loops?
Short array syntax was introduced in PHP 5.4, released in early 2012. If your version doesn't support it, you're definitely running an old, unsupported version. I strongly recommend looking into an upgrade.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: Redirect user via 301 - too many loops?
This is now erroring at the very bottom, something about:
[text]Parse error: syntax error, unexpected end of file in C:\xampp\phpMyAdmin\site\module_301.php on line 226[/text]
I've been told I need to change the URL if the catname, subname or even the title are not correct... as it can potentially cause "duplicate pages" to be cached.
I thought I had it correct. I'm stripping out the hyphens from the URL variables, and then querying that in the last section, but it isn't working. I'm getting that error.
[text]Parse error: syntax error, unexpected end of file in C:\xampp\phpMyAdmin\site\module_301.php on line 226[/text]
I've been told I need to change the URL if the catname, subname or even the title are not correct... as it can potentially cause "duplicate pages" to be cached.
I thought I had it correct. I'm stripping out the hyphens from the URL variables, and then querying that in the last section, but it isn't working. I'm getting that error.
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);
}
}
}
}
?>Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: Redirect user via 301 - too many loops?
Bingo:
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);
}
}
}
}
?>Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.