PDA

View Full Version : display url without querystring



trooperbill
02-23-2009, 02:39 PM
i want to use the new canonical rel tag to solve a sites duplicate content issues however im not that familiar with PHP.

what i need to do is display the current pages url without the querystring

anyone know how to do this? it needs to populate the following tag

<link rel="canonical" href="http://www.website.com/page.php">

where the url is page.php?id=kjhjkhasdfkjhsfdkjshdf etc

thanks
mark

JasonDFR
02-23-2009, 03:34 PM
<?php

$right = 'http://www.website.com/page.php';

$wrong = 'http://www.website.com/page.php?id=kjhjkhasdfkjhsfdkjshdf';

$wrong_after = substr($wrong, 0, strpos($wrong, '?')); // This line is the key

echo 'Right ' . $right . '<br /><br />';
echo 'Wrong: ' . $wrong . '<br /><br />';
echo 'Wrong changed: ' . $wrong_after . '<br /><br />';

// $_SERVER[] vars below might also be helpful:

echo '$_SERVER[\'PHP_SELF\']: ' . $_SERVER['PHP_SELF'] . '<br />';
echo '$_SERVER[\'REQUEST_URI\']: ' . $_SERVER['REQUEST_URI'];

?>

The above code will output:


Right http://www.website.com/page.php

Wrong: http://www.website.com/page.php?id=kjhjkhasdfkjhsfdkjshdf

Wrong changed: http://www.website.com/page.php

$_SERVER['PHP_SELF']: /page.php
$_SERVER['REQUEST_URI']: /page.php?id=kjhjkhasdfkjhsfdkjshdf

So perhaps:


<link rel="canonical" href="http://www.website.com<?php echo $_SERVER['PHP_SELF'] ?>">

Good luck,

J

trooperbill
02-23-2009, 04:33 PM
hi thanks for that :) u r a superstar

any idea how to get it to work with url rewriten paths?

trooperbill
02-23-2009, 04:37 PM
found it :)


<?php echo parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH);?>

JasonDFR
02-23-2009, 04:40 PM
That is a bit tricker.

How are you rewriting the URL? Are you using a PHP Framework? Content management application?

We posted at the same time. If that works, great. I wouldn't have known that off the top of my head.

Good luck,

J