1 个需要消除的 bug...让 PHP 生成 Canonical
为了构建一个干净的规范 URL,它总是返回 1 个基本 URL,我遇到了以下情况:
<?php
# every page
$extensions = $_SERVER['REQUEST_URI']; # path like: /en/home.ast?ln=ja
$qsIndex = strpos($extensions, '?'); # removes the ?ln=de part
$pageclean = $qsIndex !== FALSE ? substr($extensions, 0, $qsIndex) : $extensions;
$canonical = "http://website.com" . $pageclean; # basic canonical url
?>
<html><head><link rel="canonical" href="<?=$canonical?>"></head>
当 URL : http://website.com/de/home.ext?ln=de
规范:http://website.com/de/home.ext
BUT 我也想删除文件扩展名,无论是 .php、.ext .inc 还是其他两个或三个字符扩展名 .[xx]
或 .[xxx]
因此基本 url 变为:http://website.com/en/home
啊啊好多了!但我如何在当前代码中实现这一点? 非常感谢任何提示+!
for building a clean canonical url, that always returns 1 base URL, im stuck in following case:
<?php
# every page
$extensions = $_SERVER['REQUEST_URI']; # path like: /en/home.ast?ln=ja
$qsIndex = strpos($extensions, '?'); # removes the ?ln=de part
$pageclean = $qsIndex !== FALSE ? substr($extensions, 0, $qsIndex) : $extensions;
$canonical = "http://website.com" . $pageclean; # basic canonical url
?>
<html><head><link rel="canonical" href="<?=$canonical?>"></head>
when URL : http://website.com/de/home.ext?ln=de
canonical: http://website.com/de/home.ext
BUT I want to remove the file extension aswell, whether its .php, .ext .inc or whatever two or three char extension .[xx]
or .[xxx]
so the base url becomes: http://website.com/en/home
Aaah much nicer! but How do i achieve that in current code?
Any hints are much appreciated +!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
认为这应该可以做到,如果有扩展名,只需去掉末尾,就像您对查询字符串所做的那样:
Think this should do it, just strip off the end if there is an extension, just like you did for the query string:
试试这个:
$res[1] =>干净的网址;
$res[2] = 扩展名;
$res[4] = “?”之后的所有内容(如果存在且您需要的话)
try this:
and $res[1] => clean url;
$res[2] = extension;
$res[4] = everything after the "?" (if present and if you need it)