php-Magento从旧Urls重定向到具有商店(语言)代码的新Urls

前端之家收集整理的这篇文章主要介绍了php-Magento从旧Urls重定向到具有商店(语言)代码的新Urls 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

说明:
我用商店配置设置了Magento,如下所示:

>公司名称-网站

>总店-店铺

> zh-商店视图
> DK-商店视图

我的链接看起来像:https://my-company.com/shop/
然后我安慰说这是我需要的错误配置,因此我不得不进行更改

>英语-网站

>总店-店铺

> zh-商店视图

>丹麦文-Webside

>总店-店铺

> DK-商店视图

另外,我启用了以前被禁用的Magento功能“将商店代码添加到Urls”.
现在我的链接看起来像:https://my-company.com/zh / shop /

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

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

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

完整的Nginx配置:

  1. server {
  2. # Listen on port 8080 as well as post 443 for SSL connections.
  3. listen 8080;
  4. listen 443 default ssl;
  5. server_name example.com www.example.com;
  6. large_client_header_buffers 4 16k;
  7. ssl on;
  8. # Specify path to your SSL certificates.
  9. ssl_certificate /path/top/certificate.crt;
  10. ssl_certificate_key /path/to/key.key;
  11. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  12. 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";
  13. ssl_prefer_server_ciphers on;
  14. ssl_dhparam /path/to/dh_params.pem;
  15. ssl_session_cache shared:SSL:10m;
  16. ssl_session_timeout 10m;
  17. keepalive_timeout 70;
  18. add_header Strict-Transport-Security max-age=15768000;
  19. ssl_stapling on;
  20. ssl_stapling_verify on;
  21. resolver 8.8.8.8 8.8.4.4 valid=300s;
  22. resolver_timeout 5s;
  23. ssl_trusted_certificate /path/to/certificate.crt;
  24. # Path to the files in which you wish to store your access and error logs.
  25. access_log /path/to/access_log;
  26. error_log /path/to/error_log;
  27. root /path/to/root/folder;
  28. location ~* "^/(?![a-z]{2}/)(.+)$" {
  29. rewrite / /en/$1 permanent;
  30. }
  31. location / {
  32. index index.htm index.html index.PHP;
  33. try_files $uri $uri/ @handler;
  34. }
  35. # Deny access to specific directories no one in particular needs access to anyways.
  36. location /app/ { deny all; }
  37. location /includes/ { deny all; }
  38. location /lib/ { deny all; }
  39. location /media/downloadable/ { deny all; }
  40. location /pkginfo/ { deny all; }
  41. location /report/config.xml { deny all; }
  42. location /var/ { deny all; }
  43. # Allow only those who have a login name and password to view the export folder. Refer to /etc/Nginx/htpassword.
  44. location /var/export/ {
  45. auth_basic "Restricted";
  46. auth_basic_user_file htpasswd;
  47. autoindex on;
  48. }
  49. # Deny all attempts to access hidden files such as .htaccess,.htpasswd,etc...
  50. location ~ /\. {
  51. deny all;
  52. access_log off;
  53. log_not_found off;
  54. }
  55. # This redirect is added so to use Magentos common front handler when handling incoming URLs.
  56. location @handler {
  57. rewrite / /index.PHP?$query_string;
  58. }
  59. # Forward paths such as /js/index.PHP/x.js to their relevant handler.
  60. location ~ .PHP/ {
  61. rewrite ^(.*.PHP)/ $1 last;
  62. }
  63. # Handle the exectution of .PHP files.
  64. location ~ .PHP${
  65. if (!-e $request_filename) {
  66. rewrite / /index.PHP last;
  67. }
  68. expires off;
  69. fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
  70. fastcgi_pass unix:/path/to/PHP-fpm.sock;
  71. fastcgi_index index.PHP;
  72. fastcgi_param HTTPS on;
  73. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  74. fastcgi_param MAGE_RUN_CODE en;
  75. fastcgi_param MAGE_RUN_TYPE store;
  76. include fastcgi_params;
  77. }
  78. }
最佳答案
您的网站提供了三种URL:有一些漂亮的永久链接,它们不是真实的文件(例如/ en / shop /),它们是由控制器(即/index.PHP)在内部进行重写和处理的.静态内容是真实文件(CSS文件,图像,JavaScript),并由Nginx直接提供.并且有过时的站点地图(您想将其重定向到其他东西).

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

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

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

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

猜你在找的Nginx相关文章