
Nginx – Different storeviews or websites in subfolders
A client wants his multistore shop to be set up like this:
- http://www.domain1.org/en/ -> store view 2
- http://www.domain1.org/de/ -> store view 3
- http://www.domain1.org/fr/ -> store view 4
- http://www.domain1.org/it/ -> store view 5
In this post, I will show you how to do this thing by using Nginx configuration.
Table of Contents
Step 1: add the configuration mapping to Nginx’s conf file. /etc/nginx/config.d/mapping_store.conf
map $request_uri $MAGE_RUN_CODE {
default en_US;
~^/us/ en_US; #en_US is Store View 2's code
~^/de/ de_DE;
~^/fr/ fr_FR;
~^/it/ it_IT;
}
map $request_uri $MAGE_RUN_TYPE {
default store;
~^/us/ store;
~^/de/ store;
~^/fr/ store;
~^/it/ store;
}
Code language: PHP (php)
Step 2: Update the Magento’s nginx configuration
By default, Magento provides sample Nginx configuration in the website’s root directory. It doesn’t allow to run the PHP files which have a sub-folder in the request path by the line
location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check)\.php$ {
Code language: JavaScript (javascript)
You have to update this line to
location ~ (index|get|static|errors/report|errors/404|errors/503|health_check)\.php$ {
Code language: JavaScript (javascript)
When you have done previous steps, Nginx can run the PHP script from a subfolder:
http://www.domain1.ch/subfolder/phpfile.php
Step 3: Config the fastcgi_param
Now if you have run the PHP script from a subdirectory on your website. Default Magento can read the fastcgi_param. It’s using for setting the current store view or website at starting the application.
The configuration should looks like this.
Code language: PHP (php)fastcgi_param MAGE_RUN_TYPE $MAGE_RUN_TYPE; fastcgi_param MAGE_RUN_CODE $MAGE_RUN_CODE;
By adding two lines into the Nginx configuration, you are setting up the website/store-view for Magento application
# PHP entry point for main application
location ~ (index|get|static|errors/report|errors/404|errors/503|health_check)\.php$ {
try_files $uri =404;
fastcgi_pass fastcgi_backend;
fastcgi_buffers 1024 4k;
fastcgi_param PHP_FLAG "session.auto_start=off \n suhosin.session.cryptua=off";
fastcgi_param PHP_VALUE "memory_limit=756M \n max_execution_time=18000";
fastcgi_read_timeout 600s;
fastcgi_connect_timeout 600s;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PORT $http_x_forwarded_port;
fastcgi_param MAGE_RUN_TYPE $MAGE_RUN_TYPE;
fastcgi_param MAGE_RUN_CODE $MAGE_RUN_CODE;
include fastcgi_params;
}
Code language: PHP (php)
Step 4: Setting up the configuration for matching subdirectory
Those previous step to make sure you can run the PHP script includes subdirectory, now, you have to rewrite the index.php from Storeview’s directory, by adding those lines in to the Magento Nginx’s configuration file.
Code language: PHP (php)location ~ ^/(?<uri_prefix>(de|en|it|fr)) { try_files $uri $uri/ /$uri_prefix/index.php?$args; }
Step 5: Updating Magento Store View Base URL Configuration
My root website folder is /pub/
For Default config
- Base URL: http://www.domain1.ch/
- Base Link URL: https://www.domain1.ch/
- Secure Base URL: http://www.domain1.ch/
- Secure Base Link URL: https://www.domain1.ch/
- Base URL for Static View Files:http://www.domain1.ch/static/
- Base URL for User Media Files:http://www.domain1.ch/media/
- Secure Base URL for Static View Files:https://www.domain1.ch/static/
- Secure Base URL for User Media Files:https://www.domain1.ch/media/
For en
- Base URL: http://www.domain1.ch/en/
- Base Link URL: https://www.domain1.ch/en/
- Secure Base URL: http://www.domain1.ch/en/
- Secure Base Link URL: https://www.domain1.ch/en/
The Final Step: Adding index and subdirectory into the root folder
The subdirectory structure:
All done, Enjoy the new looks. Don’t forget to give me thanks 😀
3 thoughts on “Nginx – Different storeviews or websites in subfolders”
I updated the nginx.config at step 4
would this not create multiple views of the same content in Magento? That’s not good for SEO
This structure able to load difference info, you can edit the content per store view.