Search code examples
nginxopencartopencart-3

OpenCart 3 nginx config for multi language


I have a config

    location / {
        try_files $uri $uri/ @opencart;
        location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
            expires     max;
        }

        location ~ [^/]\.php(/|$) {
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            if (!-f $document_root$fastcgi_script_name) {
                return  404;
            }

            fastcgi_pass    127.0.0.1:9002;
            fastcgi_index   index.php;
            include         /etc/nginx/fastcgi_params;
        }
    }

    location @opencart {
        rewrite ^/(.+)$ /index.php?_route_=$1 last;
    }

it works fine, but I am want to add multi-language support from URL like:

location /en {
  rewrite ^/en/([^?]*) /index.php?_route_=$1&lang=en break;
}

location /es {
  rewrite ^/es/([^?]*) /index.php?_route_=$1&lang=es break;
}

Get language as the first URL param, and put it as get parameter &lang=es to index.php. How correctly do that?


Solution

  • I don't think there is a way to do that multilingual postfix through nginx (or even apache) configs. There are a lot of connections and routing around the main HTTP_SERVER (or HTTPS_SERVER) global constant. Maximum what will you get - 301 redirect on every page loading. Which is unacceptable for search engines.

    I suggest you to try tree different ways to solve this tusk:

    1. Rewrite system/library/url.php or catalog/controller/startup/startup.php. Don't have the exact code, but if you are familiar to php - the routing of URL you can find in these files. Nice manual here How to set language through url in opencart

    2. Use some free or paid modules from marketplace by query "language code in url".

    3. If You are using SEO URL - there are multilingual urls on each product, category etc. You don't need postfix with these, all URLs could multilingual and unique. enter image description here If you don't have these functions - just download it from here, it's free https://www.opencart.com/index.php?route=marketplace/extension/info&extension_id=32788