NGINX rewrite root url with language suffix

Viewed 5550

I am trying to add a simple rule to my NGINX configuration whereby the root url www.example.com is always rewritten with a language suffix www.example.com/en/. I have tried this:

server {
    listen 80;
    server_name www.example.com;

    location / {
        rewrite ^$ www.example.com/en/ permanent;
    }

    ...
}

But no luck. Where am I going wrong? Also, is it possible to have a condition where NGINX checks if there is an/en/ suffix and if not, adds one?

EDIT

So I was only one character away from what I wanted initially:

server {
    listen 80;
    server_name www.example.com;

    location / {
        # needed the / in between the anchor tags
        rewrite ^/$ www.example.com/en/ permanent;
    }

    ...
}
1 Answers
Related