简体   繁体   English

Nginx 301永久重定向以支持语言

[英]Nginx 301 Permanent Redirect For Language Support

I am now enabling language support for my web site. 我现在为我的网站启用语言支持。 I am including the language as part of the URL. 我将语言作为URL的一部分。 For example: domain.com/ en /page I need to setup 301 redirects for existing search engine indexing. 例如:domain.com/ zh / page我需要为现有搜索引擎索引设置301重定向。

The following works in Nginx to redirect from domain.com/blog to domain.com/en/blog 以下内容在Nginx中可以从domain.com/blog重定向到domain.com/en/blog

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

I don't understand the redirect needed to go from domain.com/blog/read/# to domain.com/en/blog/read/# (where # is the sequence field in a postgres database table) 我不明白从domain.com/blog/read/#到domain.com/en/blog/read/#的重定向(其中#是postgres数据库表中的序列字段)

I have spent time looking, searching and reading docs to find this answer myself. 我花时间寻找,搜索和阅读文档以自己找到答案。 I am not understanding. 我不理解。

To prefix the existing requested URI with /en you can use: 要为现有的请求URI加上/en前缀,您可以使用:

return 301 /en$request_uri;

The above will add the three characters before the existing request and also include any arguments that may be present. 上面的代码将在现有请求之前添加三个字符,还包括可能出现的任何参数。

To match any URI that begins with /blog , use location /blog { ... } . 要匹配以/blog开头的任何URI,请使用location /blog { ... } To match any URI that begins with /blog/read/ use location /blog/read/ { ... } . 要匹配以/blog/read/开头的任何URI, /blog/read/使用location /blog/read/ { ... }

Nginx chooses a location to process a request based on a set of rules . Nginx根据一组规则选择一个位置来处理请求。 So, you will need to consider the other location blocks present within your configuration. 因此,您将需要考虑配置中存在的其他location块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM