branko ajzele, senior developer / project manager

Posts / Articles

Retrieving URL paths in Magento

Magento comes with built in function for retrieving URL paths. Function is called getBaseUrl() and its located under Mage class. However, function is not documented as it should be. Documentation only states that function retrieves $type parametar which is by default equal to “base”.

Here is the list of all the available parameters I found. As you can see, they all come from Mage_Core_Model_Store.

  • Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);

    http://magesite.extension/js/

  • Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);

    http://magesite.extension/index.php/

  • Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);

    http://magesite.extension/media/

  • Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);

    http://magesite.extension/skin/

  • Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);

    http://magesite.extension/

Where http://magesite.extension/ is the url of your site. If you don’t provide any parameters to getBaseUrl() function you would retrieve the same path as with URL_TYPE_LINK parametar.

Remember, you need to echo out these statements to the browser, because they each return a string, so you need to output that string like

<?php
echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
?>

View Comments

  1. sushant danekar /

    Hey I am getting this output only…
    1) Web
    2) link
    3) MEDIA

    how to get the URL i have changed the WEB SERVER but still it taking the address of old server

    WHAT TO DO

  2. Hi sushant danekar,

    Not sure if I understood you correctly. Functions I listed are from Magento’s latest version, 1.1.4 as of time of this writing. All of the functions I listed are working, at least on my local machine.

    “i have changed the WEB SERVER but still it taking the address of old server”

    Most likely you forgot to go trough your databse and change all of the url entries. Magento stores full URL paths in database (which is, if you ask me, bad solution).

    What you need to do is export entire database to .sql file (with PhpMyAdmin) then opetn that .sql file with text editor like Notepad++ and do Find > Replace All on all of the “http://oldsite.domain/” to “http://newsite.domain”.

    After you do replace all, you save the .sql file, then import it again into the database. This should fix all of the link problems. If you encounter problem during import (problems like foreign key constraints) then read my other article published on http://activecodeline.com/2008/08/07/moving-magento-site-from-development-to-live-server/

    Feel free to contact me if you have any questions.

  3. Hi Branko this is a nice HACK :) but i’ve little different query (E’mmm may be related)
    What i want is to show up all ma CMS pages onto Tom Menu the one where by default Magento throw Categories in the form of menus. How can i add CMS pages any idea if you can help?

    Already Achieved:
    Adding a home link to menu bar:

    Add to File: app/design/frontend/default/default/template/
    catalog/navigation/top.phtml


    <a href=”getUrl(”)?>”>__(‘Home’) ?>

    Adding a Menu with all the CMS pages in dropdown:

    getStore(‘default’)->getCode();

    $cms_pages = Mage::getModel(‘cms/page’)->getCollection();
    $cms_pages->addStoreFilter($store);
    $cms_pages->load();

    echo ”.”\n”;
    echo ‘Information/Home‘.”\n”;
    echo ”.”\n”;
    $last = count($cms_pages);
    $lastText = ”;
    $ctr = 0;
    foreach($cms_pages as $_page)
    {
    $data = $_page->getData();
    if($data['identifier']==’no-route’)
    continue;

    if($ctr++ == $last)
    $lastText = ‘ last’;
    echo ‘‘.$data['title'].’‘.”\n”;
    }
    echo ”;
    ?>

    Required:

    Adding CMS pages on menu bar horizontally just like Categories

    Category-1 Category-2 About-Us Contacts Tips n Trick Team What’s new

    I’ hope you got the query…
    Looking to hear from you.

  4. Thank you very much for your feedback Qasim Ali :)

  5. branko says:
    What you need to do is export entire database to .sql file (with PhpMyAdmin) then opetn that .sql file with text editor like Notepad++ and do Find > Replace All on all of the “http://oldsite.domain/” to “http://newsite.domain”.

    After you do replace all, you save the .sql file, then import it again into the database. This should fix all of the link problems. If you encounter problem during import (problems like foreign key constraints) then read my other article published on http://activecodeline.com/2008/08/07/moving-magento-site-from-development-to-live-server/

    Feel free to contact me if you have any questions.

    hi branko, i try to do this in my website and the url still go to the oldsite, i changed all the data base but don`t work.
    maybe the URL_TYPE_LINK parametar is still with the odl site. please help me. regards.

  6. You might have missed something while doing search > replace all. If you are 100% sure you replaced everything inside the database then you might have some paths hardcoded in your template. Try search all “your old website url” on entire magento folder.

    One more thing. Try logn to Magento Admin, go to System > Configuration > Web > then choose and edit Unsecure and(or) Secure url links according to the one your site has http://somedomain.something.

  7. Hi branko, Thanks for this article.
    I have a multi store view and i’m trying to retrieve the baseURL of my website and i want to keep the store code in addition of my URLs.

    For example my domain is : http://www.domain.com, if i am on my store view ‘myview’, when i use Mage::getBaseURL() Magento gave me an url like http://www.domain.com/myview

    How can i retrieve simply the real baseUrl of my website ‘www.domain.com’ ?

    Thanks a lot.

  8. I find that
    getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB) and getBaseUrl() are different.

    If it is non-ssl, they are the same. If SSL is enabled, getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB) will return links with “https” while getBaseUrl() always return unsecured links.

  9. Thank u. this post is really helpfull.

  10. Hello Hailong!
    That would be great if true, but unfortunately they both return unsecure URLs

  11. in cms pages for store url i found {{store url=”"}} but how can i get media url tried {{media url=”"}} but didn’t work :(

  12. arun /

    Branko Ajzele !!!!!

    Your are absolutely brilliant for sharing your views…

    Your blog is very useful for us…

    Keep rocking Branko Ajzele

  13. arun /

    Branko Ajzele !!!!!Your are absolutely brilliant for sharing your views…Your blog is very useful for us…Keep rocking Branko Ajzele

  14. Thanks Branko. Your post helped me a lot.

  15. hello!

    can we get email address of admin just like we have got url. which function should i use to get email address of admin in magento

  16. [quote]I find that
    getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB) and getBaseUrl() are different. [/quote]

    getBaseUrl() defaults to URL_TYPE_LINK
    with that url type you need to specify if you want it secure or not with the second parameter, which defaults to ‘null’ (false)

    age::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK, true);

    will return the secure url

  17. Thanks, these were useful. You can also return a simple URL using the getUrl() method, which is easier in some cases.

    <?php echo $this->getUrl('desired-page'); ?>

    Gives this:

    http://magesite.com/desired-page/

  18. One should maybe add:
    With Mage::getUrl() you can get any other URL to an controller/action whatever …

  19. One should maybe add:
    With Mage::getUrl() you can get any other URL to an controller/action whatever …

  20. THANK YOU Branko! Your site saved my sanity when trying to get a Magento/Wordpress integration extension (here: http://www.sonassi.com/knowledge-base/magento-k...)
    to work.

  21. Domagoj Potkoc /

    How can I get magento base url without “http://” or “https://”

  22. sushant danekar /
  23. @Damu

    Try either {{skin url='images/media/'}} or {{store url='images/media/'}}

  24. jatinder /

    how do we apply url of root category to its sub category..eg i have a category name Jewellery and it have some sub-categories, i want, when user click on Jewellery it should redirect to one of it sub-category other then its default showing page in magento can we do that…

    and i have created a static block in magento and there i am including some custom links for user i am not able to get to redirect to proper html page…
    if user click on “necklace” link then it should redirect to jewellery/givre/necklace.html not to cms page in magento..
    can u help me on this….
    can u help me on this

blog comments powered by Disqus