Canonical URL links – osCommerce Contribution

Yesterday I launched my quick and dirty proof of concept for the implementation of the “canonical urls” link tag. Now I gathered some more information, tidied up the code and released it as a contribution. Always a great moment.
Grab the contribution here: Canonical URL link

Manual installation (for modified osCommerce shops):

  1. Open the file /catalog/includes/functions/general.php
  2. Add the following lines to general.php just before the php closing tag ( ?> ) at the bottom
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    // starts canonical tag function - www.webshoptimizer.com
    function CanonicalUrl() {
    $domain = substr((($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER), 0, -1); // gets the base URL minus the trailing slash
    $string = $_SERVER['REQUEST_URI']; // gets the url
    $search = '\&osCsid.*|\?osCsid.*'; // searches for the session id in the url
    $replace = ''; // replaces with nothing i.e. deletes
    echo $domain . ereg_replace( $search, $replace, $string ); // merges the variables and echoing them
    }
    // eof - canonical tag
  3. Open from your catalog directory index.php and product_info.php
  4. Add within your <head> section the following code to generate the correct URL:

    <link rel=”canonical” href=”<?php CanonicalUrl(); ?>” />

  5. Save your files and check your store. Done!

To test if the contribution is installed correctly, open a productpage and compare the URL in your address bar (of your browser) with the one in the source. If you look in the source the canonical tag should show the same URL as in the address bar minus the session ID.

You can also add the function (step 4) to other files if the content is of any value to your site. Files where the function (step 4) can be added:

  • product_reviews.php
  • shipping.php
  • privacy.php
  • conditions.php
  • contact_us.php

Add the function to other contributions whom add pages to your website too (e.g. newsdesk, faq, etc).

Easy installation: (only for new osCommerce v2.2RC2a installations)

  1. Upload and replace “general.php” with the one located in /catalog/inlcudes/functions/
  2. Upload and replace “index.php” and “product_info.php” with the ones located in /catalog/
  3. Done!

***** REMEMBER TO BACK UP files before making any changes ******

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • co.mments
  • eKudos
  • LinkedIn
  • StumbleUpon
  • Technorati
  • TwitThis
  • email

9 comments:

  1. Sabine, 28. February 2009, 16:04

    You’re using weird quotes, the
    $search = ā€˜\&osCsid.*|\?osCsid.*’; // searches for the session id in the url
    Line throws out errors, replacing with ‘ ‘ quotes makes it work, but takes out the l in the .nl tld.

     
  2. Sabine, 28. February 2009, 16:08

    Correction, it’s probably an issue with how your Wordpress parses the quotes.
    Still, the trailing slash code is messing up at the tld.

    Admin: Yes, Wordpress parses the quotes all wrong. Sorry about that. And it looks like in your case your server don’t put a trailing slash behind your domain name. In your case, replace the $domain line with this one:

    $domain = “http://www.yourdomain.nl”

    This will replace the variable server check with your hard coded domain name with/without trailing slash

     
  3. Lukas, 2. March 2009, 23:13

    Sorry, I see no difference with this contribution.

     
  4. Yannick, 1. April 2009, 5:42

    Same results as Lukas. URLs are not truncated.

     
  5. Cycil Borggus, 26. August 2009, 10:19

    So how do we get the link to show a correct url.
    It truncates all TLD. ie:

    Drops or cuts .net=ne, .com=co, org=or, no matter hos DNS is setup..

    ADMIN:
    In your case, replace the $domain line with this one:

    $domain = “http://www.yourdomain.nl”

    This will replace the variable server check with your hard coded domain name with/without trailing slash

     
  6. GL Customs, 21. September 2009, 20:40

    I changed the $domain to this with good results:

    $domain = HTTP_SERVER;

    There shouldn’t be a need to call the SSL as I don’t believe SSL pages will get indexed.

     
  7. GamersTronics.com, LLC, 27. October 2009, 19:33

    Here is the Solution

    okay i figured out a rough way to fix the problem.

    it looks like the author was looking to remove trailing slashes in the url. however my server doesn’t put this trailing slash so it was making my .com or .net into .co and .ne

    $domain = substr((($request_type == ‘SSL’) ? HTTPS_SERVER : HTTP_SERVER), 0, -1); // gets the base URL minus the trailing slash

    Remove the [, -1] from the code

    so it looks like this

    $domain = substr((($request_type == ‘SSL’) ? HTTPS_SERVER : HTTP_SERVER), 0); // gets the base URL minus the trailing slash

    works fine for me now.

     
  8. » Canonical URL Affiliate edition | Webshoptimizer (Pingback), 4. November 2009, 0:38
     

    [...] Get the original Canonical URL contribution [...]

     
  9. Thomas, 4. May 2010, 10:08

    To Admin – I posted this in the previous post but it might be more relevant in this one…

    I’m having the following issue after some changes in oscommerce: our shop exists in 3 languages. Product info pages are accessible through the following URLs:

    /product-name-dutch-p-1.html
    /product-name-english-p-1.html?language=dutch
    /product-name-french-p-1.html?language=french

    Will this contribution take care of this problem? Obviously I want the canonical URL be the URL in the correct language, and that the URLs in the 2 other languages with the language variable behind redirect to the first one.

    Thanks for your answer

     

Write a comment: