SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Aug 2002
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Canonical Link Element for Dynamic Pages?

    Note: This thread might also be appropriate if posted in the SEO forum but it's more of programming question, so mods feel free to move if necessary.

    If you haven't heard about the canonical link element yet go here: http://www.mattcutts.com/blog/canonical-link-tag/

    Now, I was wondering if my dynamic site has a "header.php" file which will contain the canonical link element, is there an easy way (perhaps without defining all pages in an array) to make that tag <link rel="canonical" href="http://example.com/page.html" /> dynamic enough to change according to the proper page? Any ideas? Thanks

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Yeah, i was giving this some thought after watching the video, and haven't really tried it out yet, but if you are using .htaccess as a type of controller, where say, these urls are all legal and permitted;

    website . com/recipes/Tortillas-spicy
    website . com/recipes/Tortillas Spicy
    website . com/recipes/Tortillas&#37;20Spicy
    website . com/recipes/tortillas-spicy
    website . com/recipes/Tortillas+Spicy
    website . com/Recipes/TORTILLAS Spicy

    ...and are passed onto the script and accepted, then shouldn't the page itself just contain the preferred version?

    <link rel="canonical" href="http://www. website . com/Recipes/Tortillas-Spicy" />

    Now I imagined that this would be generated and sent into a template much as any other meta tag, and that was all I had to do.

  3. #3
    SitePoint Addict
    Join Date
    Aug 2002
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is it can be cumbersome to define all possible pages right?

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Could you not lookup the supplied url against some kind of levenshtein implementation?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If Cups has a database table for recipes which looks a little something like this:

    Code:
    ID | Name
     2 | Tortillas Spicy
    Then the PHP code could be something like:
    PHP Code:
    <?php
    $Food 
    $_GET['Food'];
    $Food strtolower(str_replace(array('-''+''%20'), ' '$Food);
    So you can easily convert that back:
    PHP Code:
    <?php
    $Food 
    'tortillas spicy';
    $UrlFood str_replace(' ''-'ucwords($Food));
    And voila, you have yourself your perfect URL.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    This works for all your tortilla examples... Although this is just a quick mock-up prior to my tea.

    PHP Code:
    <?php
    $aPages 
    = array(
        
    'home'                =>    'site/home',
        
    'blog'                =>    'site/blog',
        
    'contact'            =>    'site/contact',
        
    'login'                =>    'site/login',
        
    'logout'            =>    'site/logout',
        
    'Tortillas-Spicy'    =>    'Tortillas-Spicy'
    );

    $aClosest = array(
        
    'score'    =>    0,
        
    'page'    =>    ''
    );

    foreach(
    $aPages as $sPageName)
    {
        
    $iScore similar_text($_GET['page'], $sPageName);
        if(
    $iScore $aClosest['score'])
        {
            
    $aClosest = array(
                
    'score'    =>    $iScore,
                
    'page'    =>    $sPageName
            
    );
        }
    }

    echo 
    'I would redirect you to '$aClosest['page'];

    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    An alternative to canonical links in the head would be to just 301 redirect to the perfered/pretty URLs.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  8. #8
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think the canonical link tag is supposed to replace the 301. If you can 301 to the proper place, you should definitely do it. The intended usage for this new tag is when you have duplicate content:
    www.example.com
    www.example.com/index.html
    example.com/index.html
    etc.

    Also, stripping out SID and other user variables might need to be done.
    MySQL v5.1.58
    PHP v5.3.6

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I'm not sure my point was made very clearly. All the URLs below could be legal if my controller was "kind and forgiving", as Arkinstall showed above I can do a bit of string replacement etc.

    So, all these urls should find the target on my server:

    website . com/recipes/Tortillas-spicy
    website . com/recipes/Tortillas Spicy
    website . com/recipes/Tortillas&#37;20Spicy
    www. website . com/recipes/tortillas-spicy
    website . com/recipes/Tortillas+Spicy
    www. website . com/Recipes/TORTILLAS Spicy

    (OK, there might be some OS dependencies I missed ... and I prepended www to some of the examples, for effect)

    Now in the real world, some people may have typed these urls, these urls may have been wrongly transmitted in the press, they may have been typed out by someone hearing them over the phone, they may have come out of a database and in someway been post-processed. Someone could have guessed the the sister page of Tortillas-Hot would be Tortillas-spicy. Hence there is the possibility of so many of them.

    This plays havoc with SEO and search results, because they might be treated as different results - even though all are served by the same URL.

    By setting the canonical tag in the document some search engines have now agreed to, by and large, respect that instruction and will record whatever link served up the tag to be recorded as that preferred address from now on.

    So from now on any links to

    website . com/Recipes/TORTILLAS Spicy

    will be displayed, stored, consolidated as requests for

    website . com/Recipes/Tortillas-Spicy

    Quote Originally Posted by mattcutts
    Q: What's the difference between this and a 301/perm redirect?
    A: They are very similar, but sometimes you don't have the easy ability to generate 301/permanent HTTP redirects
    Watch the video.

    But of course if you factor in the behaviour of your "kind and forgiving" controller, then adding the canonical tag is going to be much easier to do than write a 301 for every possible human error.

    To me it is important because it means that if you take the trouble to create CoolURIs, that infer meaning and are permanent, then you also have a way of stopping their meaning being diluted whilst permitting some human error.


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
  •