Nginx 301 语言支持永久重定向

Nginx 301 Permanent Redirect For Language Support

我现在正在为我的网站启用语言支持。我将语言作为 URL 的一部分。例如:domain.com/en/page 我需要为现有的搜索引擎索引设置 301 重定向。

以下在 Nginx 中可以从 domain.com/blog 重定向到 domain.com/en/blog

location = /blog {
    return 301 /en/blog;
}

我不明白从域com/blog/read/# 到域所需的重定向。com/en/blog/read/# (其中 # 是 postgres 数据库中的序列字段 table)

我自己花时间查找、搜索和阅读文档来找到这个答案。我不理解。

要在现有请求的 URI 前加上 /en 前缀,您可以使用:

return 301 /en$request_uri;

以上将在现有请求之前添加三个字符,还包括可能存在的任何参数。

要匹配任何以 /blog 开头的 URI,请使用 location /blog { ... }。要匹配以 /blog/read/ 开头的任何 URI,请使用 location /blog/read/ { ... }.

Nginx 根据 set of rules 选择一个位置来处理请求。因此,您需要考虑配置中存在的其他 location 块。