Configure multiple domain names on apache2

#1

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?

0 Likes

#2

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

#3

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

1 Like

#4

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

#5

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

0 Likes

#6

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
0 Likes

#7

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.

0 Likes

#8

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:

0 Likes

#9

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