You are here:  » Canonical code for merchants, brands, category, products, excluding sorts

Active Forum Topics


Canonical code for merchants, brands, category, products, excluding sorts

Submitted by Retro135 on Tue, 2017-01-10 04:30 in

Based on these posts...
http://www.pricetapestry.com/node/5683
http://www.pricetapestry.com/node/5973
http://www.pricetapestry.com/node/6010

...I've added this code to my header:

<?php if (isset($header["canonical"])): ?>
    <link rel='canonical' href='<?php print $header["canonical"]; ?>'>
  <?php endif; ?>
<?php
  if (isset($product["products"]))
  {
    print "<link rel='canonical' href='http://www.example.com".tapestry_productHREF($product["products"][0])."'>";
  }
  if (isset($sort) && (strpos($_SERVER["REQUEST_URI"],"&sort=")!==FALSE))
  {
    $canonical = str_replace("&sort=".$sort,"",$_SERVER["REQUEST_URI"]);
    print "<link rel='canonical' href='".$canonical."' />";
  }
?>

and to search.php:
if ($rewrite)
  {
    $header["canonical"] = "http://www.example.com".$_SERVER["REQUEST_URI"];
  }
      require("html/header.php");

It's working correctly, displays the canonical rel link for merchant, brand, category (I'm using cat hierarchy, so displays on final cat gallery page) and doesn't display for sorts.

Is there a simpler way to accomplish this?

Submitted by support on Tue, 2017-01-10 10:49

Hi,

I would leave the only modification to html/header.php as follows;

<?php if (isset($header["canonical"])): ?>
  <link rel='canonical' href='<?php print "http".($_SERVER["HTTPS"]?"s":"")."://".$_SERVER["HTTP_HOST"].$header["canonical"]; ?>'>
<?php endif; ?>

...leaving the actual value to be displayed to be set in the $header["canonical"] variable as required - I think as per the above you could end up with multiple canonical headers...

Then in products.php, look for the following code at line 64:

  $header["title"] = $product["products"][0]["name"];

...and REPLACE with;

  $header["title"] = $product["products"][0]["name"];
  $header["canonical"] = tapestry_productHREF($product["products"][0]);

For search, to re-create as you have in the above code, look for the following code at line 503:

  $header["title"] = $q;

...and REPLACE with:

  $header["title"] = $q;
  if ($rewrite)
  {
    $header["canonical"] = $_SERVER["REQUEST_URI"];
  }
  elseif($sort)
  {
    $canonical = str_replace("&sort=".$sort,"",$_SERVER["REQUEST_URI"]);
    $header["canonical"] = $canonical;
  }

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by Retro135 on Tue, 2017-01-10 13:04

Perfect! TY so much!

Submitted by Antony on Tue, 2020-06-16 13:04

Hi David,

Hope you're well,

I’m now finalizing the core part my project and I would like to ask for your advise on how to get my Canonical Links right or even perhaps Geo links?

As it stands my website has the bellow structure to support an international install.

Within each install ONLY the affiliate URLs will differ as being respective to relevant countries therefore every install would result in duplicates pages with Product and Review pages being particularly concerning as one coculd imagine.

Here is the site structure:

/css/ (shared)
/html/ (shared)
/images/ (shared)
/js/ (shared)
/uk/ (UK install)*
/us/ (US install)* - Non-existent yet
/fr/ (France install) * - Non-existent yet
/../ (for each country install)* - Non-existent yet
index.php (Independant TL/home)
robots.txt

*each install folder then of course hold all core files including .htaccess
** Every geo install will in most case offer the same “products (offer)” only with a respective unique geographic URL, absolutely no other variance in content.

The Price Tapestry structure has been heavily modified from original version to accommodate the above and I really don’t want to make a mess out of it and of course I would like to benefit from your professional skills for the best possible seo.

Any chance for support there? I would be happy to share any credentials you may need if this would make it easier to look into.

Let me know of any questions,

Many thanks,

Antony

Submitted by support on Wed, 2020-06-17 07:07

Hi Antony,

I wasn't quite sure what stage you are at with your implementation of the above / where you wanted to implement canonical URLs. Could you be a little more specific, for example an actual URL (i'll remove before publishing your reply), and the canonical URL you wanted that page to have and I'll try and point you in the right direction...

Thanks,
David.
--
PriceTapestry.com

Submitted by Antony on Wed, 2020-06-17 09:52

Hi David,

Hope you're well,

As the site will only be published in English (UK) regardless of jurisdictions, I think it might be best to point all canonical URLs towards the UK install.

{code saved}

Should point to the UK install accordingly towards:

{code saved}

This is the current Robots.txt where for each install I plan to copy/paste and modify (uk) for each other countries

And this is the current Htaccess within each installations (/uk/ will of course be unique to each installations:

{code saved}

I think thats about it; I hope this is sufficient information but please let me know if you require more info; again I would be happy to share my hosting credential if you want to dig in :)

I really-really appreciate your support.

Many thanks,

Antony

Submitted by support on Wed, 2020-06-17 12:24

Hi Antony,

If you wanted to apply a global canonical to all non-/uk/ installations you could do something like this within the <head> section output by html/header.php:

<?php
  
if ($config_baseHREF != "/uk/")
  {
    print 
"<link rel='canonical' href='".str_replace($config_baseHREF,"/uk/",$_SERVER["REQUEST_URI"])."' />";
  }
?>

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by Antony on Wed, 2020-06-17 13:21

Hi David,

This appears to be prescisely what I need. Thank you so much.

Ant

Submitted by Antony on Wed, 2020-09-23 06:36

Hi David,

I would like to add to the above a Self-Referencing canonical to specify that "this is" the canonical page whenever the UK pages are being crawled.

Something like:

<?php
  if ($config_baseHREF != "/uk/")
  {
    print "<link rel='canonical' href='".str_replace($config_baseHREF,"/uk/",$_SERVER["REQUEST_URI"])."' />";
    else {
    print "<link rel='canonical' href='???' />";
  }
?>

Is there a simple way to do this?

Thanks,

Antony

Submitted by support on Wed, 2020-09-23 07:31

Hi,

In that case, no need for the IF condition - /uk/ can simply "replace" itself - so just:

   print "<link rel='canonical' href='".str_replace($config_baseHREF,"/uk/",$_SERVER["REQUEST_URI"])."' />";

Cheers,
David.
--
PriceTapestry.com

Submitted by Antony on Wed, 2020-09-23 09:13

Hi David,

Leaving the above as-is results in the UK pages NOT having a canonical link at all

For instance in the 2 links bellow where one is CA and the other UK:

{links saved}

These are the same pages (Just different currency and Aff-links) we can see the CA page canonical pointing the the UK one which is great but it does seem not render anything at all for the UK page as header.php conditions to render a link only if NOT within the UK with: if ($config_baseHREF != "/uk/")

Although not crucial, Google now recommends that if a page is unique in itself it should still have a self canonical link referencing to it own.

Any second thoughts?

Kindest,

Antony

Submitted by support on Wed, 2020-09-23 09:31

Hi Antony,

That looks like the containing IF condition is still in place, so if you currently still have:

  if ($config_baseHREF != "/uk/")
  {
    print "<link rel='canonical' href='".str_replace($config_baseHREF,"/uk/",$_SERVER["REQUEST_URI"])."' />";
  }

...REPLACE with just:

  print "<link rel='canonical' href='".str_replace($config_baseHREF,"/uk/",$_SERVER["REQUEST_URI"])."' />";

That should be all it is...

Cheers,
David.
--
PriceTapestry.com

Submitted by Antony on Wed, 2020-09-23 11:13

Of course I see the point here.

Thank you David much appreciated!

Ant