How to get / read a clean URL in Meta Canonical via PHP?

Considering: old ugly URLs, such as /somepage?ln=en, are rewritten in htaccess to /en/somepage
Given: the canonical meta tag is used, with this php script above it, to fill in a neat URL:

How to make them look like canonical?

      <link rel="canonical" href="<?=$canonicalURL?>">

How can I parse the current URL without any lines or remove extra lines from the URL and put it in the canonical URL?

+3
source share
2 answers

Essentially, you just want to get rid of the query string from $ extensions, fix it?

<?php
$qsIndex = strpos($extensions, '?');
$extensions = $qsIndex !== FALSE ? substr($extensions, 0, $qsIndex) : $extensions;
+1
source
$url = parse_url('http://example.com/path/page?param=value');

print_r($url)

Array
(
    [scheme] => http
    [host] => example.com
    [path] => /path/page
    [query] => param=value
)

Then you can simply do:

$url['scheme'] . '://' . $url['host'] . $url['path']

Or even:

$url = 'http://example.com/path/page?param=value'; 
'http://example.com' . parse_url($url, PHP_URL_PATH)
+3
source

Source: https://habr.com/ru/post/1780901/


All Articles