Magento从旧网址重定向到具有存储(语言)代码的新网址

时间:2015-11-24 15:11:32

标签: php regex magento url nginx

说明:
我用这样的商店配置设置了Magento:

  • 公司名称 - Webside
    • 主要商店 - 商店
      • zh_ - 商店视图
      • dk - 商店视图

我的链接如下:https://my-company.com/shop/

然后我重申,这是我的需求的错误配置,我不得不改变

  • 英文 - Webside
    • 主要商店 - 商店
      • zh_ - 商店视图
  • 丹麦语 - Webside
    • 主要商店 - 商店
      • dk - 商店视图


加号,<启用 Magento功能&#34; 将商店代码添加到网址&#34;以前是禁用的。
现在我的链接如下:https://my-company.com/ zh / shop /



问题:
由于我已经制作了站点地图而没有进行更改并将其提交给WebMasters,现在我面临的问题是,所有没有商店代码的旧链接都会再次工作( 404代码 - 未找到。)


由于WebMaster和其他原因,我真的想实现这个结果:
当有人试图打开其中一个没有商店代码的旧网址时(例如https://my-company.com/shop/),我希望通过添加商店代码作为网址中的第一个网址来重定向到新网址


我已经尝试通过在我的nginx配置中添加一些重写规则来实现这一点,但是我遇到了无限循环,最后我找不到正确的重写规则解决方案。 (链接到我的nginx重写规则问题:Nginx Config Location Regex With Language Code In Url

完整Nginx配置:

server {
    # Listen on port 8080 as well as post 443 for SSL connections.
    listen              8080;
    listen              443 default ssl;

server_name         example.com www.example.com;

large_client_header_buffers 4 16k;

ssl             on;

# Specify path to your SSL certificates.
ssl_certificate         /path/top/certificate.crt;
ssl_certificate_key     /path/to/key.key;

ssl_protocols               TLSv1 TLSv1.1 TLSv1.2;

ssl_ciphers         "EECDH+AESGCM:EDH+AESGCM:ECDHE-RSA-AES128-GCM-SHA256:AES256+EECDH:DHE-RSA-AES128-GCM-SHA256:AES256+EDH:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";

ssl_prefer_server_ciphers   on;
ssl_dhparam         /path/to/dh_params.pem;
    ssl_session_cache       shared:SSL:10m;
    ssl_session_timeout     10m;
    keepalive_timeout       70;
    add_header          Strict-Transport-Security max-age=15768000;

ssl_stapling            on;
ssl_stapling_verify     on;
resolver            8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout        5s;

ssl_trusted_certificate     /path/to/certificate.crt;

# Path to the files in which you wish to store your access and error logs.
access_log          /path/to/access_log;
error_log           /path/to/error_log;

root                /path/to/root/folder;

location ~* "^/(?![a-z]{2}/)(.+)$" {
    rewrite         / /en/$1 permanent;
}

location / {
    index           index.htm index.html index.php;
    try_files       $uri $uri/ @handler;
}

# Deny access to specific directories no one in particular needs access to anyways.
location /app/          { deny all; }
location /includes/     { deny all; }
location /lib/          { deny all; }
location /media/downloadable/   { deny all; }
location /pkginfo/      { deny all; }
location /report/config.xml { deny all; }
location /var/          { deny all; }

# Allow only those who have a login name and password to view the export folder. Refer to /etc/nginx/htpassword.
location /var/export/ {
    auth_basic      "Restricted";
    auth_basic_user_file    htpasswd;
    autoindex       on;
}

# Deny all attempts to access hidden files such as .htaccess, .htpasswd, etc...
location ~ /\. {
    deny            all;
    access_log      off;
    log_not_found       off;
}

# This redirect is added so to use Magentos common front handler when handling incoming URLs.
location @handler {
    rewrite         / /index.php?$query_string;
}

# Forward paths such as /js/index.php/x.js to their relevant handler.
location ~ .php/ {
    rewrite         ^(.*.php)/ $1 last;
}

# Handle the exectution of .php files.
location ~ .php$ {
    if (!-e $request_filename) {
        rewrite     / /index.php last;
    }

    expires         off;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass        unix:/path/to/php-fpm.sock;
    fastcgi_index       index.php;
    fastcgi_param       HTTPS on;
    fastcgi_param       SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param       MAGE_RUN_CODE en;
    fastcgi_param       MAGE_RUN_TYPE store;
    include         fastcgi_params;
}

}

2 个答案:

答案 0 :(得分:3)

您的网站有三种网址:有一些非真实文件(例如/en/shop/),它们由内部重写并由控制器处理(即/index.php)。静态内容是真实文件(CSS文件,图像,javascript),由nginx直接提供。并且有一个过时的站点地图(您想要重定向到其他地方)。

问题是您添加的重写规则也与静态内容匹配。

因此,在测试重定向之前,您需要先提供静态内容。这是通过让try_files查看原始URI然后在@handler块中应用新的重写规则来实现的:

location @handler {
  rewrite "^/(?![a-z]{2}/)(.+)$" /en/$1 permanent;
  rewrite / /index.php?$query_string;
}

这似乎适用于我的测试环境,但是YMMV

答案 1 :(得分:0)

这个代码我为我的nginx配置添加,也许对其他人有帮助,它也是修复管理区域:

location / {
 rewrite "^/(?![a-z]{2}/)(.+)$" /en/$1 permanent;
  rewrite / /index.php?$query_string;
}

location /index.php/admin {
 try_files $uri $uri/ /index.php?$args;
 index index.php index.html index.htm;
}