Hello,
I have an e-commerce site that has various category pages that let the customer sort the products shown by all the standard sorting orders (sort by name, price, rating, model, etc.).
The sorting works fine to the end user. However when I open my site in Google Webmaster Tools, I get a lot of duplicate content errors for these pages.
For example, the following page has identical content, but can be sorted many different ways. I receive the following duplicate title tags warning in Webmaster Tools:
/category?sort=p.model&order=DESC
/category?sort=pd.name&order=DESC
/category?sort=rating&order=DESC
I successfully modified the tags depending on how the page gets sorted (not the issue I’m writing about here). However, I’m also trying to set a canonical link to let Google know which page (/category) should be the preferred URL to index.
As background on how the code below works:
$_GET[‘sort’] will give one of the following sort methods: p.sort_order, pd.name, p.price, rating, or p.model
$_GET[‘order’] will either produce ASC, or DESC
$_SERVER[“REQUEST_URI”] produces something like /category?sort=rating&order=DESC depending on what the sort and order variables are.
I’m trying to use str_replace to replace the “?sort=xxx&order=yyy” with a single “/” in the canonical URL. Ideally, every canonical URL should be identical for the same category page, leaving me with the following, regardless of how the page gets sorted:
<link rel="canonical" href="http://mysite.com/category/" />
Instead, I get:
<link rel="canonical" href="http://mysite.com/category?sort=xxx&order=yyy" />
I’m not sure where I’m going wrong in the code. I’m sure its a small simple fix, but I can’t seem to figure it out.
[php]<?php $replace_url = ($_SERVER["REQUEST_URI"]) ?>
<?php if ($_GET['sort'] = 'p.sort_order' && $_GET['order'] = 'ASC') { ?>
<link rel=“canonical” href=“http://mysite.com<?php echo str_replace("?sort=p.sort_order&order=ASC","/",$replace_url); ?>” />
<?php } elseif ($_GET['sort'] = 'p.sort_order' && $_GET['order'] = 'DESC') { ?>
<link rel=“canonical” href=“http://mysite.com<?php echo str_replace("?sort=p.sort_order&order=DESC","/",$replace_url); ?>” />
<?php } elseif ($_GET['sort'] = 'pd.name' && $_GET['order'] = 'ASC') { ?>
<link rel=“canonical” href=“http://mysite.com<?php echo str_replace("?sort=pd.name&order=ASC","/",$replace_url); ?>” />
<?php } elseif ($_GET['sort'] = 'pd.name' && $_GET['order'] = 'DESC') { ?>
<link rel=“canonical” href=“http://mysite.com<?php echo str_replace("?sort=pd.name&order=DESC","/",$replace_url); ?>” />
<?php } elseif ($_GET['sort'] = 'p.price' && $_GET['order'] = 'ASC') { ?>
<link rel=“canonical” href=“http://mysite.com<?php echo str_replace("?sort=p.price&order=ASC","/",$replace_url); ?>” />
<?php } elseif ($_GET['sort'] = 'p.price' && $_GET['order'] = 'DESC') { ?>
<link rel=“canonical” href=“http://mysite.com<?php echo str_replace("?sort=p.price&order=DESC","/",$replace_url); ?>” />
<?php } elseif ($_GET['sort'] = 'rating' && $_GET['order'] = 'ASC') { ?>
<link rel=“canonical” href=“http://mysite.com<?php echo str_replace("?sort=rating&order=ASC","/",$replace_url); ?>” />
<?php } elseif ($_GET['sort'] = 'rating' && $_GET['order'] = 'DESC') { ?>
<link rel=“canonical” href=“http://mysite.com<?php echo str_replace("?sort=rating&order=DESC","/",$replace_url); ?>” />
<?php } elseif ($_GET['sort'] = 'p.model' && $_GET['order'] = 'ASC') { ?>
<link rel=“canonical” href=“http://mysite.com<?php echo str_replace("?sort=p.model&order=ASC","/",$replace_url); ?>” />
<?php } elseif ($_GET['sort'] = 'p.model' && $_GET['order'] = 'DESC') { ?>
<link rel=“canonical” href=“http://mysite.com<?php echo str_replace("?sort=p.model&order=DESC","/",$replace_url); ?>” />
<?php } else { ?>
<?php } ?>
[/php]