I'm trying to achieve something with nginx and redirect rules which seems like it should be pretty straightforward, but I've run into a stumbling block.
Having had a look through many questions and answers, I can't seem to find a solution that works for me.
What I want to achieve is the following:
If someone navigates to my website with any of the following URLs:
http://mywebsite.com
http://mywebsite.com/
http://mywebsite.com/foo
the person will be re-directed to:
http://mywebsite.com/en/
http://mywebsite.com/en/
http://mywebsite.com/en/foo/
This is what I have in my nginx configuration file so far in the order in which I have them in the file:
location ^/en/(.*) {
try_files $uri /index.php$is_args$args last;
}
location / {
if ( $uri !~ ^/(index\.php|css|jpg|png|images|core|uploads|js|robots\.txt|favicon\.ico) ) {
return 301 /en/$uri;
}
}
However, with the above configuration, I am running into an infinite redirect loop and the URL in the address bar ends up like this before the server gives up:
http://mywebsite.com/en//en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/
Can someone please:
a) Explain to me why my configuration has not had the intended effect so I can understand it and not repeat the same errors in the future
b) Propose a solution that works and if possible improve upon my configuration
c) If possible, take it further and explain with examples how I can get nginx to automatically determine the locale associated with the request and dynamically transform the request to include it in the fashion described above.
Thanks