Magento从旧网址重定向到具有存储(语言)代码的新网址 [英] Magento Redirect From Old Urls To New Urls Which Has Store (Language) Code

查看:42
本文介绍了Magento从旧网址重定向到具有存储(语言)代码的新网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说明:
我用以下存储配置来设置Magento:

The Description:
I set up Magento with store config like this:

  • 公司名称-网站
    • 主要商店-商店
      • zh_CN -商店视图
      • dk -商店视图
      • Company Name - Webside
        • Main Store - Store
          • en - Store View
          • dk - Store View

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

          然后我重新确认这是我需要的错误配置,我必须进行更改

          My links looked like: https://my-company.com/shop/

          Then I reaslised that this was wrong config for my needs and I had to change

          • 英语-网络端
            • 主要商店-商店
              • zh_CN -商店视图
              • English - Webside
                • Main Store - Store
                  • en - Store View
                  • 主要商店-商店
                    • dk -商店视图
                    • Main Store - Store
                      • dk - Store View


                      ,我启用了 Magento功能"将商店代码添加到Urls ",该功能先前已被禁用.
                      现在我的链接看起来像: https://my-company.com/ en /shop/


                      Plus, I enabled Magento feature "Add Store Code to Urls" which was previously disabled.
                      Now my links look like: https://my-company.com/en/shop/



                      问题:
                      由于我已经在未进行更改的情况下制作了站点地图,并将其提交给WebMaster,所以现在我面临的问题是,所有没有在url中存储代码的旧链接都不再有效(不再了( 404代码-找不到).



                      Problem:
                      Since I already made sitemap while the changes were not made and submitted it to WebMasters, now I am facing the problem that all of the old links without store code in url does not work anymore (404 code - not found).



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



                      Because of WebMasters and other reasons I would really like to achieve this Result:
                      When somebody tries to open one of the old urls which is without store code (e.g. https://my-company.com/shop/), I would like he is redirected to the new url by just adding the store code as the first segment in url.



                      我已经尝试通过向nginx配置中添加一些重写规则来实现此目的,但是我遇到了无限循环,最后我找不到正确的重写规则解决方案. (链接到我的Nginx重写规则问题: Nginx配置位置正则表达式在网址中添加语言代码)

                      完整的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;
                      }
                      
                      }
                      

                      推荐答案

                      您的网站字段提供了三种URL:有些漂亮的永久链接不是真实文件(例如/en/shop/),它们在内部被重写和处理.控制器(即/index.php).静态内容是真实文件(CSS文件,图像,javascript),直接由nginx提供.还有一个过时的站点地图(您想将其重定向到其他地方).

                      Your web site fields three kinds of URL: There are the pretty permalinks which are not real files (e.g. /en/shop/) which get internally rewritten and handled by a controller (i.e. /index.php). There is the static content which are real files (CSS files, images, javascript) and are served directly by nginx. And there is the obsolete sitemap (which you want to redirect to something else).

                      问题在于您添加的重写规则也与静态内容匹配.

                      The problem is that the rewrite rule you added also matches the static content.

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

                      So, you need to serve the static content first, before testing for the redirect. This is achieved by letting try_files see the original URI and then applying your new rewrite rule, inside the @handler block:

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

                      这似乎可以在我的测试环境中使用,但是YMMV

                      This seems to work in my test environment, but YMMV

                      这篇关于Magento从旧网址重定向到具有存储(语言)代码的新网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆