在nginx-config中的下面一行中,重写从例如:www.example.com到www.example.com/en。
rewrite ^/$ /$lang/ redirect; 不起作用的是重定向,例如:www.example.com/test到www.example.com/en/test
如何才能使这些重定向?
发布于 2022-08-30 20:16:13
我终于解决了它,没有regex。
在服务器块中,首先处理/de、/en和任何其他可用语言,最后是/的位置,这使得重定向到其中一种语言,重定向请求位于其中一个语言位置。
server {
location /de {
# /de Rules here
}
location /en {
# /en Rules here
}
location / {
# $scheme:// for "http://" or "https://"
# $host for the domain-name, like "example.com"
# /$lang for the inserted lang-code like "/de" or "/en"
# $request_uri for everything after the domain-name. From the original-question this would be "/test" (It comes with a slash in the front).
return 301 $scheme://$host/$lang$request_uri;
}
} 下面的代码仅用于说明如何确定$lang以避免混淆。nginx配置的第一个文本检查浏览器语言,它可以被名为"language“的cookie覆盖,该cookie可以包含"de”或"en“之类的值。
map $http_accept_language $hal_lang {
default en;
~de de;
~en en;
}
map $cookie_language $lang {
default $hal_lang;
~de de;
~en en;
}https://stackoverflow.com/questions/73528052
复制相似问题