用于多语言的 OpenCart 3 nginx 配置

OpenCart 3 nginx config for multi language

我有配置

    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_= last;
    }

它工作正常,但我想从 URL 添加多语言支持,例如:

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

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

获取语言作为第一个 URL 参数,并将其作为获取参数 &lang=es 到 index.php。 如何正确地做到这一点?

我认为没有办法通过 nginx(甚至 apache)配置来实现多语言后缀。主要HTTP_SERVER(或HTTPS_SERVER)全局常量周围有很多连接和路由。您将获得的最大收益 - 每次加载页面时进行 301 重定向。这对搜索引擎来说是不可接受的。

我建议你尝试树不同的方法来解决这个象牙:

  1. 重写system/library/url.phpcatalog/controller/startup/startup.php。没有确切的代码,但如果您熟悉 php - URL 的路由,您可以在 这些文件。不错的手册在这里

  2. 通过查询使用来自市场的一些免费或付费模块 "language code in url"。

  3. 如果您正在使用 SEO URL - 每个产品、类别等都有多语言 URL。您不需要这些后缀,所有 URL 都可以多语言和独特的。 如果您没有这些功能 - 只需从这里下载,它是免费的 https://www.opencart.com/index.php?route=marketplace/extension/info&extension_id=32788