Setting up Magento with multiple websites or stores

There are many tutorials how to set Magento to work with multiple stores and make different domains point at each store. Since release of Magento CE 1.4.0.0-beta1 and Magento EE 1.6.0.0 it is even more easy to do.

Magento evolves

Solutions used in previous versions required developer to modify index.php file to handle different domains pointing at different stores. New index php contains following code:

$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';

Mage::run($mageRunCode, $mageRunType);

So it checks two environmental variables and use them to start Magento runing. What does it give you? You can set now which store/website is supposed to be running under selected domain directly in virtual host definition or even htaccess.

VirtualHost solution

To benefit from this little piece of code it is enough for you to add following lines within your VirtualHost definition:

SetEnv MAGE_RUN_CODE "base" # put here your website or store code
SetEnv MAGE_RUN_TYPE "website" # put here 'website' or 'store'

.htaccess solution

If you have no access to virtual host definitions, you can still try to use .htaccess for that, putting within following lines:

SetEnvIf Host .*yourhost.* MAGE_RUN_CODE=base
SetEnvIf Host .*yourhost.* MAGE_RUN_TYPE=website

Where .*yourhost.* is an regex expression matching the domain for which you want to set environmental variable.

So now you are capable of setting up your Magento multiple stores website without messing up with the core. Good luck.

34 comments on "Setting up Magento with multiple websites or stores"

Rich's picture
Rich (visitor) - Sun, 10/01/2010 - 02:33:

I understand the code, I see the new field in the back-end, but why is type "website" or "store"?

Is this suppose to be set whether it goes to the Website or Store View?

Lucjan Wilczewski's picture
Lucjan Wilczewski - Tue, 12/01/2010 - 15:09:

@Rich: You are always on the Store View, but you may be on particular one, or default one. If you will use 'website' and will give Website code you will land on specified Website's default store's default Store View. If you will use 'store' and will give Store View code, then you will find yourself on the specified Store View.
As a matter of fact there is also a 'group' type valid, which would lead to default Store View for the Store, and it seems that code you have to provide in this case is the Store's (store_group) id value.

Dan Shields's picture
Dan Shields (visitor) - Thu, 11/02/2010 - 21:47:

I don't know if this is for every type of setup, but I found that for the .htaccess method you have to remove the quotes around the values and also the semi-colon like so

SetEnvIf Host .*yourhost.* MAGE_RUN_CODE=base
SetEnvIf Host .*yourhost.* MAGE_RUN_TYPE=website

Conec's picture
Conec (visitor) - Fri, 05/03/2010 - 16:25:

Hi, I am busy an all day and night trying to get this fixed and I do not succeed! Can you please help me?

I want to get URL's to website's

In advance thanks for your help!

conec's picture
conec (visitor) - Sat, 06/03/2010 - 21:50:

Hi, I am on Magento 1.4.0.1

Thanks for this post, I almost lost my hairs on that.

I made that on index.php:

$mageRunCode = isset($_SERVER[’MAGE_RUN_CODE’]) ? $_SERVER[’MAGE_RUN_CODE’] : ‘newbesite’;
$mageRunType = isset($_SERVER[’MAGE_RUN_TYPE’]) ? $_SERVER[’MAGE_RUN_TYPE’] : ‘website’;

And turn “Redirect to Base URL if requested URL doesn’t match it” to “NO” in the admin

It works for me to get to my “newwebsite”, but now I can get to my “base” website! or to other websites!
So how and what to configure for multi websites?

Regards.

Laurent Clouet's picture
Laurent Clouet - Mon, 08/03/2010 - 11:37:

Hi Conec,

What code did you put in your .htaccess file? What is URL or your base and newwebsite?

For example if URL of you base is www.base.com and URL of your newwebsite is www.newwebsite.com you should put following code in your .htaccess

SetEnvIf Host .*base.* MAGE_RUN_CODE="base";
SetEnvIf Host .*newwebsite.* MAGE_RUN_TYPE="newwebsite";

Regards

Mike's picture
Mike (visitor) - Wed, 17/03/2010 - 17:38:

Hi, Mag 1.4.0.1 I'm on a dedicated server.

I have set up my first 10 websites in Magento and pointed all 10 urls to my nameserver. In addition, I have set up each store within the admin to the correct urls. I tested them and they are all landing on the main domain, so I think I'm to this step now (correct me if I'm wrong please).

1. How would I change my index.php for multiple domains? Is this the best way to do multiple domains? In the end I might have close to 300 domains pointing to one instance of Magento.

2. I was told to park all of my domains under the Main domain in Cpanel where Magento is installed. Is this recommended?

3. Will these domains be able to share the same cart under one SSL?

Obviously, I'm new to all of this but trying to make my way through it so any feedback would be great. I think once I get the first few set up the other 200+ will be easy. I just want to make sure I get started in the right direction.

jai's picture
jai (visitor) - Thu, 01/04/2010 - 17:54:

version 1.4.0.1
It did not work. Can some one guide me how to add multiple sites. I dont know mush about it and if anyone can send steps then I will really indebted.

hardly urgent. pls help me.

Ste's picture
Ste (visitor) - Thu, 08/04/2010 - 16:18:

I’m currently running 2 websites from one Magento installation and I want to enable Web Server Rewrites for SEO. However, when I do this, I’m getting a 404 Not Found error for the page I’m navigating to. It works fine when rewrites are turned off.

Rewrites are enabled in Apache and rewrites do work on the base website. It just wont work on the external websites.

Any ideas?

Alex's picture
Alex (visitor) - Wed, 28/04/2010 - 03:35:

Here another simple solution:

if ($_SERVER['HTTP_HOST'] == "someotherdomain.com"){
$_SERVER["MAGE_RUN_CODE"] = "yourcode";
$_SERVER["MAGE_RUN_TYPE"] = "website";
}

before:

$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';

Note: I set up the second store as website - not as store.

Daniel's picture
Daniel (visitor) - Sun, 16/05/2010 - 14:59:

Hi Mike,

I was wanting to know if you ever got this working? I am trying to do the exact setup as you are.
I have successfully set up a multi store environment, but I am unable to share the cart. I know I have something wrong and was hoping you could shed some light on this for me.
We have our own server and have setup the different Url's to point to the root document of the main site. Then I have the other domains parked.

In my index.php I have something like this....

switch($_SERVER['HTTP_HOST']) {
case 'site1.com':
case 'www.site2.com':

$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : 'site1';
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
Mage::run($mageRunCode, $mageRunType);
break;

Can you tell me how you did it?

frank's picture
frank (visitor) - Wed, 19/05/2010 - 17:52:

hi, guys,
do anybody solve this ?
i am losing my hair everyday right now.
waiting for solutions....

frank's picture
frank (visitor) - Fri, 21/05/2010 - 09:29:

Hi ,
I have the problem like this(my service: debian+apache2+php5.26+...): and i am going to set different website to the same bacekend but with different IPs and SSL.
1,newly installed magento 1.4.0.1 on "domain1.com" with code "base", this domain has IP "IP1"
2,and then am i setup another visul host "domain2.com" , which come with IP "IP2",
3,add new website to the back end with code "domain2"
4,as the expeirence of http://www.crucialwebhost.com/blog/how-to-setup-multiple-magento-stores/, i did copy index.php and .htaccess to the root fold of domain2.com .../domain2/public_html/
5, reate symbolic links to point to the folds of /domain1/public_html/
6,then i add the code to ... /domain2/public_html/.htaccess

SetEnvIf Host .*domain1.com.* MAGE_RUN_CODE="domain1";
SetEnvIf Host .*domain2.com.* MAGE_RUN_TYPE="domain2"
;

and now when i try to access to domain2.com, i got:
app/Mage.php was not found

Here i am totally new to magento, so can you please help to tell me where is the problem ? or did i do not need to step4-6, and only need to add the code to ../domain1/public_html/. htaccess ?

Thank you in advance.

Frank

Anonymous's picture
Anonymous (visitor) - Fri, 04/06/2010 - 11:25:

Hi,

I don't understand your method
In my example i have one domain : domain.com
i create two store on the same domain : domain.com

what is the procedure for this configuration

Thank you in Advance

Hayden's picture
Hayden (visitor) - Wed, 07/07/2010 - 16:23:

I am also having trouble with this. I have one domain with multiple stores setup. I have one Magento installation setup on my domain (www.example.com). I have setup two stores: retail, wholesale. I want my primary domain to be the retail store and would like my wholesale store to be a subdomain (wholesale.example.com). How do I setup my Magento Installation to do this? Thanks in advance.

Jonathan Day's picture
Jonathan Day (visitor) - Mon, 09/08/2010 - 06:41:

I found the same, it's necessary to remove the double quotes AND the semi-colon

sunil's picture
sunil (visitor) - Wed, 11/08/2010 - 15:10:

i have installed magento om my primary domian like Maindomain.com/ now the url of this main domian is Maindomain.com/StoreviewCode/Category/SubCategory.its fine for me. now i have also create a another add on domain def.com on server.now how can i make the url for this add on domin like def.com/Category/subcategory i dont want to include this StoreviewCode in add on domain url.please provide the solution how can i do it..thanks so much

Peter's picture
Peter (visitor) - Fri, 20/08/2010 - 16:04:

I am struggling with this for a few days now and tried every option discripted here. Unfortunatly I am not able to get everything working. I have got working 1 basestore and one substore, there is just 1 session made when I want to order on both of them, but I still have to seperate carts. I am working with version 1.4.1.0.. Does someone have a suggestion to try?

Thank you in advance.

Peter

mariek's picture
mariek (visitor) - Tue, 05/10/2010 - 20:16:

Maybe you have an idea:
In CPanel is the new domain2 installed as an addon domain.
I installed magento(single install 1.4) After that I created the second website with store and storeview and it has its own root directory.

Also I have switched the unsecure and secure base url link to {{unsecure_base_url}}domain2/ and created a new folder with index.php and .htaccess

After that I put this code in my .htaccess file in the root directory: RewriteCond %{HTTP_HOST} ^(.*)domain2.com
RewriteRule ^ - [E=MAGE_RUN_CODE:codeofdomain2]

I have the error 404 page not found now on domain2.com
SO what to do next?? Do I have to change some snippet in the index.php? I tried nearly everything...

Raphael's picture
Raphael (visitor) - Tue, 09/11/2010 - 02:55:

Hi,

I got a problem. The store is switching but i do Not get a Session id.

What do im wrong?

Magento 1.4.1.0

frustrated's picture
frustrated (visitor) - Sun, 05/12/2010 - 08:27:

I have followed almost every tutorial available for this.

My domain1.com is working fine.

My domain2.com can load pages. It displays the categories assigned to this site. If you search for products, the products assigned to this site appear. But as soon as you click on a category or a product, you immediately receive a 404 error.

Magento: 1.4.0.1
I've created a second domain, "added-on" it to my first domain, copied .htaccess and index.php and moved them to the folder of the second domain.

I've tried adding lines to .htaccess like suggested above, as well as the code given by Adrian to for the index.php file.

But still :(

Has anyone had this problem with the 404?

krite's picture
krite (visitor) - Tue, 07/12/2010 - 09:06:

I have put this code does not know that it is correct or not. .htaccess If not, how should suggest a solution thanks.

[code]############################################
## If running in cluster environment, uncomment this
## http://developer.yahoo.com/performance/rules.html#etags

#FileETag none

############################################

SetEnv MAGE_RUN_CODE health123 # put here your website or store code
SetEnv MAGE_RUN_TYPE http://www.health123.com/ # put here 'website' or 'store'

[/code]

[b]in my host[/b]

code]
SetEnvIf Host .*yourhost.* MAGE_RUN_CODE=health123;
SetEnvIf Host .*yourhost.* MAGE_RUN_TYPE=http://www.health123.com/;
[/code]

[b]Remark : as defined in the code health123 Manager Store => website name => code.
[/b]

and In My index.php

[code]
/* Store or website code */
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';

/* Run store or run website */
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';

Mage::run($mageRunCode, $mageRunType);
[/code]

[

please help me again

david bill's picture
david bill (visitor) - Thu, 13/01/2011 - 06:40:

We have the same setup -- main site with two other domains which point to subdirectories that were created in the store root, so when the url is requested, it goes to the subfolders instead of the root folder and bypasses the .htaccess file where these rules are stored. Can anyone give me advice on how to solve that

michaeldf's picture
michaeldf (visitor) - Fri, 21/01/2011 - 21:22:

ok. this method seemed to work the best with the .htacess file.

i am with godaddy,
shared hosting account.
different url's
1.4.2

.htacess at around line 130 just after rewritebase /

I placed the following.

RewriteCond %{HTTP_HOST} ^(.*)site1.com
RewriteRule ^ - [E=MAGE_RUN_CODE:rs_site1code]

RewriteCond %{HTTP_HOST} ^(.*)mainstore.com
RewriteRule ^ - [E=MAGE_RUN_CODE:base]

where site1.com is www.site1.com
where mainstore.com is www.mainstore.com
where rs_site1code is the code for he webstore.

index.php modification is.

../app

Im going to do a full set of instructions on magentocommerce when i finish making 1.4.2 work. because if anything the last two weeks has taught me that there is no real set of instructions on how to set up multiple stores using different variables. quite erking when you are trying to do this the first time.

Gui's picture
Gui (visitor) - Wed, 09/02/2011 - 17:29:

Does anyone have any experience with setting up multiple stores on subdirectories (mysite.com/a, mysite.com/b, etc.) under the one ‘website’?

If so, what is the difference between following this blog's method, and alternatively following the alternative advice stated here for example:
http://www.magentocommerce.com/blog/comments/multi-store-retailing-magen...
see comment no.13)?

The latter suggests that NO physical subdirectories are actually created but that they are “mimicked” by simply “setting Add Store Code to Urls to Yes in the Url Options section [and] Set the Secure and Unsecure URLs back to default.”

On the other hand, I believe that this blog's method - and similarly at http://www.crucialwebhost.com/blog/how-to-setup-multiple-magento-stores/... -
entails actually creating physical subdirectories as well as modifying the index and htaccess files.

Can anyone explain the different rationales between these two methodologies please? Very confused!
Many thanks

Anonymous's picture
Anonymous (visitor) - Tue, 22/02/2011 - 18:18:
Hedda's picture
Hedda (visitor) - Fri, 18/03/2011 - 18:48:

No matter what, I still keep coming to this nice post, thanks to all of you :D

Magento Loewe's picture
Magento Loewe (visitor) - Fri, 01/04/2011 - 10:13:

Wow, i tried and tried and tried but it seems it will not work.
After some hours, close to depression i found my Mistake XD

Just forgot a ` in a line!! .. Damn little Signs :D

Thanks a lot for your introduction, it even helps for a german starter.

go on that way

george's picture
george (visitor) - Thu, 05/05/2011 - 09:08:

I have done the modifications you mentioned for running multiple domains single admin.

But when i go to the new store url(http://www.store.com/), it gets redirected to http://www.store.com/downloader

Please help me in fixing this....

Michael Hostetler's picture
Michael Hostetler (visitor) - Sat, 25/06/2011 - 11:24:

I am considering doing a similar install one Magneto package to handle my three online stores, however it took me quite a bit of customization to bring one of my stores to function as part of my hosted SharePoint CRM. I'm wondering if I switch all these stores to one version of Magneto will that either, make them all connect to my CRM or in the worst case force me to re-do all my hosted SharePoint customizations?

Jakob's picture
Jakob (visitor) - Sat, 16/07/2011 - 23:33:

Hi, nice post!

I got this to work with one of my domains right away!

But i have a problem, i have a domain containing the character "æ" and i can't get that domain to go to other then the main store, i have tried to set the domain to the ASCII friendly domain name xn--domain-cdb.dk but that didn't work ether.

Any suggestions to a solution?

Cyberscripts's picture
Cyberscripts (visitor) - Wed, 10/08/2011 - 20:15:
Magento Designer's picture
Magento Designer (visitor) - Wed, 07/12/2011 - 14:31:

Great post. I found what I was looking for. Do you mind if I post this on my website and give you credit? If not, it’s ok.
http://www.2jdesign.co.uk/

Custom Web Design's picture
Custom Web Design (visitor) - Wed, 22/02/2012 - 07:07:

Hi,
I am working in Magento but when i am trying to setup a blog inside Magento i get error.
please guide me.