Attempting to create canonical links depending on PHP page sort order


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? /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,, 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="" />

Instead, I get:

<link rel="canonical" href=";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=“<?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=“<?php echo str_replace("?sort=p.sort_order&order=DESC","/",$replace_url); ?>” />
<?php } elseif ($_GET['sort'] = '' && $_GET['order'] = 'ASC') { ?>
<link rel=“canonical” href=“<?php echo str_replace("?","/",$replace_url); ?>” />
<?php } elseif ($_GET['sort'] = '' && $_GET['order'] = 'DESC') { ?>
<link rel=“canonical” href=“<?php echo str_replace("?","/",$replace_url); ?>” />
<?php } elseif ($_GET['sort'] = 'p.price' && $_GET['order'] = 'ASC') { ?>
<link rel=“canonical” href=“<?php echo str_replace("?sort=p.price&order=ASC","/",$replace_url); ?>” />
<?php } elseif ($_GET['sort'] = 'p.price' && $_GET['order'] = 'DESC') { ?>
<link rel=“canonical” href=“<?php echo str_replace("?sort=p.price&order=DESC","/",$replace_url); ?>” />
<?php } elseif ($_GET['sort'] = 'rating' && $_GET['order'] = 'ASC') { ?>
<link rel=“canonical” href=“<?php echo str_replace("?sort=rating&order=ASC","/",$replace_url); ?>” />
<?php } elseif ($_GET['sort'] = 'rating' && $_GET['order'] = 'DESC') { ?>
<link rel=“canonical” href=“<?php echo str_replace("?sort=rating&order=DESC","/",$replace_url); ?>” />
<?php } elseif ($_GET['sort'] = 'p.model' && $_GET['order'] = 'ASC') { ?>
<link rel=“canonical” href=“<?php echo str_replace("?sort=p.model&order=ASC","/",$replace_url); ?>” />
<?php } elseif ($_GET['sort'] = 'p.model' && $_GET['order'] = 'DESC') { ?>
<link rel=“canonical” href=“<?php echo str_replace("?sort=p.model&order=DESC","/",$replace_url); ?>” />
<?php } else { ?>

<?php } ?>

i’m sure in thinking regardless of making this a canonical link the content will still be duplicated which will get you penalised by google. your best bet is to get google to ignore the variable ?sort= and order= and then it will only use the catagory not the way it is sorted. canonical links are useful but not for specifying that it is unique in the way it is sorted as the content will still be duplicate

Sponsor our Newsletter | Privacy Policy | Terms of Service