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):
- Open the file /catalog/includes/functions/general.php
- Add the following lines to general.php just before the php closing tag ( ?> ) at the bottom
// 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
- Open from your catalog directory index.php and product_info.php
- Add within your <head> section the following code to generate the correct URL:
<link rel="canonical" href="<?php CanonicalUrl(); ?>" />/li>
- 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)
- Upload and replace “general.php” with the one located in /catalog/inlcudes/functions/
- Upload and replace “index.php” and “product_info.php” with the ones located in /catalog/
- Done!
***** REMEMBER TO BACK UP files before making any changes ******
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.
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
Sorry, I see no difference with this contribution.
Same results as Lukas. URLs are not truncated.
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
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.
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.
[…] Get the original Canonical URL contribution […]
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
I am clearly missing something with this osCommerce contribution. What else does it do, other than remove the SID from the URL?
I ask because if a store is set up correct, a bot shouldn’t be starting a session. Thanks.
Hi Pete,
You are right. Search engines SHOULDN’T be indexing those session id’s, but even Google wants to be naughty once in a while and index them anyway. You CAN’T prevent SID from being indexed when a customer decides to link to a specific URL with (unfortunately) the SID. There are still a lot URL’s with SID indexed, this addon prevents it by just removing the SID. For filters, variables and sessions is even why Google introduced the canonical tag in the first way.
hi matt,
i added this contribution on the my oscommerce site. it adds the canonical tag, but my title and meta description are gone. where do i add this info? how do i fix this?
thanks
e