0

我可以将哪些 PHP 或 Javascript 代码用于模板上的规范链接?我尝试使用下面的 PHP 代码,但它不起作用:

<?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;
?>
4

2 回答 2

1

改变

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

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

或者

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

不用说到底什么不起作用,这就是我可以发现/假设的问题。

您的代码将产生:<link rel='canonical' href='http://www.hea-employment.com' . abitrary . />由于您从不关闭第一个“ . 只是一个.字符而不是连接。

你的第二个街区也有同样的问题。

于 2012-05-17T22:59:34.520 回答
0

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;
于 2013-01-08T18:20:01.823 回答