我已经搜索了SO,但是找不到确切的答案。
生成URL非常简单...
像这样:
<link rel="canonical" href="https://example.com<?php echo ($_SERVER['REQUEST_URI']); ?>" />
但是,与此有关的问题是,$_SERVER['REQUEST_URI'])
将始终获取正在使用的当前文件,因此规范URL可能会发生变化。
因此它可能会在www.example.com/hello.php和www.example.com/hello/以及其他许多变体之间切换,具体取决于用户访问您网站的方式。
我如何使它始终是相同的URL? (最好没有.php)
答案 0 :(得分:0)
我自己做了,很基本:
<?php
$fullurl = ($_SERVER['REQUEST_URI']);
$trimmed = trim($fullurl, ".php");
$canonical = rtrim($trimmed, '/') . '/';
?>
然后...
<link rel="canonical" href="https://example.com<?php echo $canonical ?>" />
我敢肯定有不同的方法,但这对我有用。
答案 1 :(得分:0)
这就是我的工作。
<?php
// get the rigth protocol
$protocol = !empty($_SERVER['HTTPS']) ? 'https' : 'http';
// simply render canonical base on the current http host ( multiple host ) + requests
echo $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
?>
答案 2 :(得分:0)
我认为您的脚本需要进行一些消毒,对吗? 我的意思是,如果您的页面是
https://example.com/test.php
但是一个恶意但无害的人会这样做
https://example.com/test.php/anotherThing.php
您的规范将成为
https://example.com/anotherThing.php
不过,您不希望这种情况发生,对吗?特别是如果恶意的人不是无害的并且对您的网址做了最坏的事情......