【问题标题】:Nginx language redirect: /de/test/test.php to /test/test.php?Nginx 语言重定向:/de/test/test.php 到 /test/test.php?
【发布时间】:2012-09-22 13:41:00
【问题描述】:

我正在尝试在新服务器上配置 Nginx。我有许多 PHP 脚本(例如 /test/test.php),我想“按原样”使用这些脚本(默认语言,英语),以及语言重定向。示例 - 当“/de/test/test.php”被请求时,

  • nginx 写入 cookie (lang=de)
  • 并返回“/test/test.php”(不修改URI,使访问者停留在“/de/test/test.php”

非常感谢任何帮助!我已经为此失去了几个晚上,我已经绝望到要取消新服务器并返回共享主机。

谢谢!

【问题讨论】:

    标签: redirect nginx


    【解决方案1】:

    在你的 php 位置下创建一个子位置:

    location ~ ^.+\.php$ {
        # Return '400 Bad Request' for malformed URL exploits
        location ~ \..*/.*\.php$ {
            return 400;
        }
        location ~ ^/de/(.+)\.php {
            add_header Set-Cookie lang=de;
            rewrite ^/de/(.+)\.php$ /$1.php last;
        }
        # Your proxy or fastcgi code to process PHP
        ...
    }
    

    【讨论】:

      【解决方案2】:

      我知道它很旧,但如果其他人觉得它有用,下面是我解决它的方法(在朋友的帮助下)。我在 nginx 中设置了 LANG 变量,然后我在 PHP $_SERVER['LANG'] 中选择了它。

      # languages
      location ~ ^/(af|sq|ar|hy|az|be|bs|bg|cs|zh|da|de|nl|el|en|et|fa|fi|fr|ka|he|hi|hr|hu|id|it|ja|kk|ko|lv|lt|nb|no|pl|pt|ro|ru|sk|sl|es|sr|sv|th|tk|tr|uk|uz|vi|bp)/ 
      { 
          set                         $lang $1; 
          rewrite                     ^/[^/]+/(.+)$ /$1 break; 
          fastcgi_pass                127.0.0.1:9000; 
          fastcgi_index               index.php; 
          fastcgi_intercept_errors    on; 
          fastcgi_read_timeout        600; 
          include                     fastcgi_params; 
          fastcgi_param               SCRIPT_FILENAME $document_root$fastcgi_script_name; 
          fastcgi_param               LANG $lang; 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-16
        • 1970-01-01
        • 2017-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-23
        相关资源
        最近更新 更多