Our blog

 

Magento Remove Hard Coded URLs from Theme

If you are working on a theme, you may find that there are hard coded links dotted all over the place.

This can make testing a site offline (and thereby not on the same URL) a bit of a pain.

Here is a quick fix for you:

1. Remove all hard coded links from the CMS section:

SQL:
  1. UPDATE cms_block SET content = REPLACE(content, 'http://www.domain.com/', '{{store url=''}}');

SQL:
  1. UPDATE cms_page SET content = REPLACE(content, 'http://www.domain.com/', '{{store url=''}}');

2. Remove all hard coded links from the theme files.

This is a command line one using grep and sed.

Firstly, cd into the root theme folder

CODE:
  1. cd app/design/frontent/default/hardtheme

Now find files with hard coded links

CODE:
  1. grep -rl 'http://www.domain.com' ./

Now to automatically remove all those,

CODE:
  1. grep -rl 'http://www.domain.com' ./ | xargs sed -i 's/http:\/\/domain.com\//<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);?>/'

More Reading:

 

Leave a Reply