1 个需要消除的 bug...让 PHP 生成 Canonical

发布于 2024-10-12 18:09:38 字数 826 浏览 8 评论 0原文

为了构建一个干净的规范 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别理我 2024-10-19 18:09:38

认为这应该可以做到,如果有扩展名,只需去掉末尾,就像您对查询字符串所做的那样:

$pageclean = $qsIndex !== FALSE ? substr($extensions, 0, $qsIndex) : $extensions;
$dotIndex = strrpos($pageclean, '.');
$pagecleanNoExt = $dotIndex !== FALSE ? substr($pageclean, 0, $dotIndex) : $pageclean; 
$canonical = "http://website.com" . $pagecleanNoExt;   # basic canonical url

Think this should do it, just strip off the end if there is an extension, just like you did for the query string:

$pageclean = $qsIndex !== FALSE ? substr($extensions, 0, $qsIndex) : $extensions;
$dotIndex = strrpos($pageclean, '.');
$pagecleanNoExt = $dotIndex !== FALSE ? substr($pageclean, 0, $dotIndex) : $pageclean; 
$canonical = "http://website.com" . $pagecleanNoExt;   # basic canonical url
下雨或天晴 2024-10-19 18:09:38

试试这个:

preg_match("/(.*)\.([^\?]{2,3})(\?(.*)){0,1}$/msiU", $_SERVER['REQUEST_URI'], $res);
$canonical = "http://website.com" . $res[1];

$res[1] =>干净的网址;
$res[2] = 扩展名;
$res[4] = “?”之后的所有内容(如果存在且您需要的话)

try this:

preg_match("/(.*)\.([^\?]{2,3})(\?(.*)){0,1}$/msiU", $_SERVER['REQUEST_URI'], $res);
$canonical = "http://website.com" . $res[1];

and $res[1] => clean url;
$res[2] = extension;
$res[4] = everything after the "?" (if present and if you need it)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文