1. Home
  2. Prices
  3. Guided Tour
  4. New Shop
  5. Shop Review
  6. Ecommerce Marketing
  7. Ecommerce TV
  8. Magento Developers
  9. Portfolio
  10. Blog
  11. Contact
Home > Magento Tutorials > Making Links Local in Magento

Making Links Local in Magento

Posted on: 16th Jun 2010 By: Luci Smethurst Leave a comment 3 Comments

One of the lovely built-in issues with Magento is that all the links it generates are external – ie it will always preface the link with the domain, like this: http://www.domain.co.uk/category.html. This is a Bad Thing in terms of SEO – and what’s the point of having a brilliant e-commerce web design if the search engines don’t like it? Don’t worry, a solution can be found right here on the Magento blog. Read on, avid readers!

The solution is actually relatively simple – just add a string replace onto any links you would like to be local, for example on the getChildHtml include in your head.phtml file:

echo str_replace("http://www.domain.co.uk/", "/", $this->getChildHtml());

- replace www.domain.co.uk with your domain name and it will replace all instances of it with a /.

However, all is not well in the getCssJsHtml include. This little function is the reason why it’s not as simple as it could be – it pulls in all the CSS, JS and HTML tags that you see in the tag, which sounds brilliant in theory, but if you have enabled canonical links it’s actually a Bad Thing.

Why, you ask?

Because the wonderful string replace tags we used earlier will find all instances of your domain name (for example www.e-commercewebdesign.co.uk) and replace them with a / – including in the generated canonical link tags.

All is not lost however. We’ll just separate the likes of the canonical links from the Css, Js and other Html tags… like two naughty children in a playground.

  • Go to your getCssJsHtml() function
  • Find the section helpfully labelled “other stuff” (about line 211), copy the code and delete it from the function:
	// other stuff
            if (!empty($items['other'])) {
                $html .= $this->_prepareOtherHtmlHeadElements($items['other']) . "\n";
            }

            if (!empty($if)) {
                $html .= '<!--[endif]-->'."\n";
            }
  • Create a new function, I’ve called my getOtherHeadLinks()
  • Paste in the code you’ve just copied
  • Now, that code isn’t enough on it’s own, we also need to tell all the whither-to’s and why-fors, so we add some more code above the “other stuff” so that you end up with the function below, and hey presto, you’ve got a working function and separate canonical links!
  public function getOtherHeadLinks()
    {
        // separate items by types
        $lines  = array();
        foreach ($this->_data['items'] as $item) {
            if (!is_null($item['cond']) && !$this->getData($item['cond']) || !isset($item['name'])) {
                continue;
            }
            $if     = !empty($item['if']) ? $item['if'] : '';
            $params = !empty($item['params']) ? $item['params'] : '';
            switch ($item['type']) {
                default:
                    $this->_separateOtherHtmlHeadElements($lines, $if, $item['type'], $params, $item['name'], $item);
                    break;
            }
        }

        // prepare HTML
           $html   = '';
        foreach ($lines as $if => $items) {
            if (empty($items)) {
                continue;
            }
            if (!empty($if)) {
                $html .= '<!-- [if '.$if.']-->'."\n";
            }

			// other stuff
            if (!empty($items['other'])) {
                $html .= $this->_prepareOtherHtmlHeadElements($items['other']) . "\n";
            }

            if (!empty($if)) {
                $html .= '<!--[endif]-->'."\n";
            }

        }
        return $html;
    }
  • Twitter
  • AIM
  • WordPress
  • LinkedIn
  • Facebook
  • StumbleUpon
  • Technorati Favorites
  • Delicious
  • Ping
  • Digg
  • Bebo
  • Reddit
  • MySpace
  • NewsVine
  • Google Bookmarks
  • Sphinn
  • Propeller

3 Responses to “Making Links Local in Magento”

  1. Amelia

    Great Stuff, Luci. It’s fantastic to see your contribution to the ecommerce blog. Thanks for sharing :)

  2. Ecommerce Web Design

    Really great little article, I’ve already started to test this on one of our magento sites so far so good

  3. Michael Angrave

    Good advice! thanks Luci!

Post A Comment