Configure multiple domain names on apache2

i am trying to set up 2 website (with 2 different domain names) to run on the same Magento 2 back end.
i created the 2 website,store,store view entries, than created two virtual hosts on the apache2 config file under sitesavailable. the problem i am having is setting up these two Magento environment variables:

MAGE_RUN_TYPE
MAGE_RUN_CODE

and tying everything up together.

Could you please help me?

Good question. I might do video on thisā€¦ one day.

So, it seems like you have already done most of the heavy lifting :+1:

Iā€™ve not tested this in Magento 2, but Iā€™m pretty sure it would work this way:

Example of Stores Setup


Example of Apache Config File 1 (Port 80)

<VirtualHost *:80>
    DocumentRoot /var/www/html
    ServerName exampleone.com
    SetEnv MAGE_RUN_TYPE "exampleone"
    SetEnv MAGE_RUN_CODE "english"
</VirtualHost>

Example of Apache Config File 2 (Port 80)

<VirtualHost *:80>
    DocumentRoot /var/www/html
    ServerName exampletwo.com
    SetEnv MAGE_RUN_TYPE "exampletwo"
    SetEnv MAGE_RUN_CODE "english"
</VirtualHost>

Therefore, Apache would check what URL the visiting was trying to access (exampleone.com or exampletwo.com)ā€¦ Then reroute the connection to the specific Web Site / Store View

Magento Official Documentation:

  • MAGE_RUN_TYPE can be either store or website
    • Use website to load a website in your storefront.
    • Use store to load any store view in your storefront.
  • MAGE_RUN_CODE is the unique website or store view code that corresponds to MAGE_RUN_TYPE

If it doesnā€™t work, itā€™ll be a few days before I get a chance to try this myself. Let us know how you get on.

1 Like

Thank you for your help.
this worked perfectly for me. cheers

1 Like

Hi Craig,

I spotted a possible glitch in the examples above.
As per the Magento documentation you included, the MAGE_RUN_TYPE variable should be either website or store. In your example I think it should be website instead of english. Thatā€™s how I am using it anyway.

1 Like

Good shout. Iā€™ve edited the answer. Still not tested it yet, but if you have thatā€™s good enough for me.

I see you switched the two variables :slight_smile: I might have been unclear. I meant the following:

<VirtualHost *:80>
    DocumentRoot /var/www/html
    ServerName exampleone.com
    SetEnv MAGE_RUN_TYPE "website"
    SetEnv MAGE_RUN_CODE "exampleone"
</VirtualHost>

If it might help someone ā€¦
The way I am using this on Nexcess is by adding the second domain as a pointer and add this on top of my .htaccess file in my html root directory:

SetEnvIf Host .*exampleone\.com.* MAGE_RUN_TYPE=website
SetEnvIf Host .*exampleone\.com.* MAGE_RUN_CODE=exampleone

SetEnvIf Host .*exampletwo\.com.* MAGE_RUN_TYPE=website
SetEnvIf Host .*exampletwo\.com.* MAGE_RUN_CODE=exampletwo

In my example, the Code for the Store View for both of the Websites are ā€œenglishā€. So, I did need to swap the variables around as you mentioned. But those variables relate to the screenshot example. If that makes sense? Although, I will admit that this is all worked out in my head until I actually test and document the real thing.

Quick heads up about the .htaccess fileā€¦ Whenever you upgrade Magento, it replaces the .htaccess file with a default copy from the repository. When possible, save your edits in the Apache/NginX Config. Having said that, I did read somewhere once that you can configure Composer to never update the .htaccess file. But Iā€™ve not looked into that yet. Currently, I still go in and put my amendments back after each upgrade - But that seems inefficient and easy to overlook.

I see what you mean. What Iā€™m trying to say is that the MAGE_RUN_TYPE variable should have a value of either store or website (literally), since it denotes what entity type the MAGE_RUN_CODE is referring to. Now in your example it has the value exampleone or exampletwo (the ā€˜codeā€™ of a website).

As far as I can tell from the documentation, you also canā€™t use a store view code for MAGE_RUN_CODE. Only a website or store code (since these are the MAGE_RUN_TYPE options). I hope I am making some sense here :slight_smile:

Thanks for the warning. I wasnā€™t aware of this. Might have to see whether I can access the NGINX settings on Nexcess, but I doubt it since I am using the shared SIP 100 package. Iā€™ll try to keep it in mind though :wink:

Okay, I see what youā€™re saying. Iā€™ll bookmark this topic and revisit after Iā€™ve had a play around.

As for the .htaccess thing, having Composer skip the file might be a better fit. I think you have to edit the composer.json file in the Magento root directory, but honestly I canā€™t remember. If you find out, please let me know.

EDIT: Maybe not

1 Like