开发者

Nginx language redirect: /de/test/test.php to /test/test.php?

开发者 https://www.devze.com 2023-04-11 23:58 出处:网络
I\'m trying to configure Nginx on a new server. I have a number of PHP scripts (f.e. /test/test.php) and I want to use this scripts \"as is\" (default language, English), as well as with language redi

I'm trying to configure Nginx on a new server. I have a number of PHP scripts (f.e. /test/test.php) and I want to use this scripts "as is" (default language, English), as well as with language redirecting. Example - when "/de/test/test.php" is requested,

  • nginx writes a cookie (lang=de)
  • and returns "/test/test.php" (without modifying URI, so that visitor remains on "/de/test/test.php"

Any help is greatly appreciated! I already lo开发者_开发技巧st several nights fighting with this, and I'm getting desperate enough to cancel new server and return back to shared hosting.

Thanks!


Create a sub location under your php location:

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
    ...
}


I know it's old but in case anyone else finds it useful, below is how I solved it (with some help from a friend). I set LANG variable in nginx, which I then pick up in 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; 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号