user1402136
user1402136

Reputation: 1

Canonical link for template pages

What PHP or Javascript code can I use for a canonical link on my templates? I tried to use the PHP code below by it isn't working:

<?php
$qs = $_SERVER['QUERY_STRING'];
$page = $_SERVER['REQUEST_URI'];
if(strlen(trim($qs))==0){
 // No query string is present
 $canlink = "<link rel='canonical'
href='http://www.hea-employment.com' . $page . />";
}else{
 // Query string is present
 $canlink = "<link rel='canonical'
href='http://www.hea-employment.com' . $page . "?" . $qs . />";
}
echo $canlink;
?>

Upvotes: 0

Views: 314

Answers (2)

Geo
Geo

Reputation: 13026

1) Your quotes are messed up a bit.

2) Why create a canonical link to self? Here's what I suggest for you to use:

<?php

$qs = $_SERVER['QUERY_STRING'];
$page = $_SERVER['REQUEST_URI'];
$canlink = '';

// If query string is present, add a canonical link to avoid duplicate content
if (strlen(trim($qs))) {
    $canlink = "<link rel='canonical' href='http://www.hea-employment.com$page' />";
}
echo $canlink;

Upvotes: 0

tigrang
tigrang

Reputation: 6767

Change

$canlink = "<link rel='canonical' href='http://www.hea-employment.com' . $page . />";

to

$canlink = "<link rel='canonical' href='http://www.hea-employment.com{$page}'/>";

or

$canlink = '<link rel="canonical" href="http://www.hea-employment.com' . $page . '"/>';

Without saying what exactly isn't working, that's what I can spot/assume is the issue.

Your code will produce: <link rel='canonical' href='http://www.hea-employment.com' . abitrary . /> Since you never close the first " the . is just a . char and not concatenation.

Same issue for your second block.

Upvotes: 1

Related Questions