1 bug to kill... Letting PHP Generate The Canonical.

Posted by Sam on Stack Overflow See other posts from Stack Overflow or by Sam
Published on 2011-01-17T18:39:34Z Indexed on 2011/01/17 18:53 UTC
Read the original article Hit count: 216

Filed under:
|
|
|
|

Hi folks, 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 +!

(other advices for proper canonical usage in this multi-lingual environment are welcome as well)

© Stack Overflow or respective owner

Related posts about php

Related posts about html