Ron Piggott
Ron Piggott

Reputation: 705

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. For example: domain.com/en/page I need to setup 301 redirects for existing search engine indexing.

The following works in Nginx to redirect from domain.com/blog to 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)

I have spent time looking, searching and reading docs to find this answer myself. I am not understanding.

Upvotes: 0

Views: 372

Answers (1)

Richard Smith
Richard Smith

Reputation: 49702

To prefix the existing requested URI with /en you can use:

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 { ... }. To match any URI that begins with /blog/read/ use location /blog/read/ { ... }.

Nginx chooses a location to process a request based on a set of rules. So, you will need to consider the other location blocks present within your configuration.

Upvotes: 1

Related Questions