Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get/read the cleanest url into Meta Canonical via PHP?

Given: URLs like /somepage?ln=en are rewritten in htaccess to /en/somepage
Given: used canonical Meta tag, with this PHP script above it to fill in the tidy URL:

How to make them like the canonical?

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

What ways can one parse the current URL without any strings, or, delete the extra strings from the curl and put it into the canonical URL?

like image 327
Sam Avatar asked Dec 02 '25 05:12

Sam


2 Answers

$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 could just 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)
like image 143
Ben Avatar answered Dec 03 '25 19:12

Ben


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

<?php
$qsIndex = strpos($extensions, '?');
$extensions = $qsIndex !== FALSE ? substr($extensions, 0, $qsIndex) : $extensions;
like image 33
simshaun Avatar answered Dec 03 '25 17:12

simshaun