Results 1 to 5 of 5

Thread: display url without querystring

  1. #1
    Join Date
    Jun 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default display url without querystring

    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

  2. #2
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    PHP Code:
    <?php

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

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

    $wrong_after substr($wrong0strpos($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:

    Code:
    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:

    PHP Code:
    <link rel="canonical" href="http://www.website.com<?php echo $_SERVER['PHP_SELF'?>">
    Good luck,

    J
    Last edited by JasonDFR; 02-23-2009 at 03:50 PM.

  3. #3
    Join Date
    Jun 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    hi thanks for that u r a superstar

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

  4. #4
    Join Date
    Jun 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    found it

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

  5. #5
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •