Magento snippet for switching between websites

Featured Image

Assuming you have set unique base URL foreach website running on the same Magento installation all you need to switch between those websites is this little snippet.
Just paste it somewhere in your templates.

However, keep in mind that I only tested this to work for my own configuration.

Basicly, what this snippet does is redirect to website’s default store base url.

<!-- BEGIN website switcher -->
<select id="website-changer" onChange="document.location=this.options[selectedIndex].value">
< ?php
$websites = Mage::getModel('core/website')->getCollection();
foreach($websites as $website)
    $default_store = $website->getDefaultStore();
    $url_obj = new Mage_Core_Model_Url();
    $default_store_path = $url_obj->getBaseUrl(array('_store'=> $default_store->getCode()));
    <option <?php if(strstr($this->helper('core/url')->getCurrentUrl(), $default_store_path)):?>selected="selected"< ?php endif; ?> value="< ?php echo $default_store_path ?>">< ?php echo $website->getName()?></option>
< ?php
<!-- END website switcher -->

This code comes with apsolutely no warranty, however I wish somebody will find it usable. ๐Ÿ˜€
If you have any questions, you can ask here.

EDIT: As always WP has some code formating issues, I hope that’s not a problem ๐Ÿ˜‰

About Tomas Novoselic

Team Leader/Backend Developer

At Inchoo, Tomas is a Team Leader and Certified Magento Developer who handles Magento modifications at any level. He also works closely with clients on Magento projects of any size and difficulty.

Read more posts by Tomas / Visit Tomas's profile


  1. Hi @Thomas,

    Is there any way to to replace & with & in magento url. Every time when magento URL is redirecting from one store to another it is adding & between the url. And the url is breaking for a custom CMS page.
    for eg:

    Above URL is the default url which is linked to US storeview based on US geoIP. When I am clicking on the url from Google search results it should redirect me to the same page with respective storeview. Now below url is the one when I clicked on google search result.
    And the above link is breaking.

    When I am making a small change & to & like below then my url is working fine and showing correct page

    I gone through some tutorials but I have not get any solution for the same.

  2. the following module allows you to implement store switcher in Magento, has advance feature, check out

  3. I have the same kind of problem that chad has. If i change website with the dropdown and new website loads on browser, the selected website (in the dropdown) is not the one I’m actually on. For some reason the website that is shown as selected in the dropdown is the default website that is set up in Magento configurations.

    Any ideas how to fix this? It seems to be related to url rewrites. If you turn them off from magento configurations the dropdown works as it should and the right selection is selected when page loads.

  4. Thanks to your post, i’ve installed the multiple website successfully. But, I have a problem now when switching to another store, the dropdown-select resets to the base_website.
    The scenario is this, user selected the other website, and click log-in, the URL turns to https from http.
    Then the dropdown select return to the base_website but he is on the correct website selected.
    The dropdown is the only thing that misses/malfunctioning.
    Thanks in advance for any help/feedback.

  5. If i have a website_id, how can i get all store_id associated with it? e.g. if i have id for ‘main website’ how can i get ids for (english,french,german)

  6. Hi Turko, if you mean to redirect to the user directly to the right website, it can be done by htaccess, and redirect the user to a defined URL, based on his ip.

    You can search in the web “redirect based on ip by htaccess”. I dont think it will take you a long time to find it out.
    However, if you do this, I think that user will not able to go to the other websites…

  7. Hi,

    Do you know how can I do the following:
    I have a magento store with three websites in it.
    When a user log in, he has to log in as well in each of the other sites…. I would like to be able to log in, in all websites with only one login, is it possible ?


  8. Hi, great website for ways to work in Magento

    Im looking for ways to run multiple stores with multiple domains under a magento go single back end?


  9. can u tell me about the folder structure to create two website each with 2 store and each store having 2 store views

  10. Hi Tomas,

    When you say “Just paste it somewhere in your templates.” would you provide a more specific suggestion as to which file should I paste this snippet of code.


  11. Is there anything else I need to do to get this working besides copying and pasting the code into my theme file? I added it to the header.phtml file of my template, and it makes the entire page into a blank white screen.

    I’m thinking that my multiple websites might not be set up correctly. Any suggestions?

  12. Apologies, I have found that information. I hard-coded the main link into the header.phtml for the h1 logo class and that worked.

  13. I ended up creating landing pages for my two stores and using text on those pages to switch between stores. Could somebody tell me how to change the link for the header logo to revert to the main index instead of to the current store? I tried using a hard coded link in header.phml, but it didn’t change anything, maybe because of the .htaccess file?

  14. Hi, I must say that the Inchoo site makes understanding Magento easier.

    When you say to put the above snippet “somewhere in your templates,” could you please be a little more specific?

    Thank you so much!

  15. @Robert

    You can get array of store codes
    (ones you enter at “Store View Information” menu in admin panel) using $website->getStoreCodes();

    All other is pretty much the same. You will need loop or two, adjust this snippet a little, but in general I think thats the way ๐Ÿ™‚

  16. @Ben,

    We waited for you to solve this and then used our spy in your company to steal the code and publish it here ๐Ÿ˜€

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.