Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have a site spanish.com. I want users to load pages under "es" directory, i.e., spanish.com/es. For example:

Users should see: spanish.com but spanish.com/es is loaded

Users see spanish.com/samplepage but spanish.com/es/samplepage is loaded

I tried

server {
    listen 80;
    server_name www.spanish.com;
    return 301 $scheme://spanish.com$request_uri;

server {
    listen 80;
    server_name spanish.com;
    limit_conn arbeit 32;

    rewrite ^(.*)$ http://$host/es/$1 permanent;

    ## Access and error logs.
    access_log /var/log/nginx/spanish.com_access.log;
    error_log /var/log/nginx/spanish.com_error.log;


but it results to infinite redirects to http://spanish.com/es/es/es/es/es/es/es/...

also, how do you exclude files from the rewrite rule? Like in apache?

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
share|improve this question

1 Answer 1

This is how I got through it:

## for the homepage
location = / {
    rewrite ^ /es;

## for the rest of the pages, excluding locations beginning
## with /<2char-lang-prefix>/*
## also excludes locations having only 3 characters, e.g. "/nb"
location ~* "^/(?!.{2,2}/).{3}.*$" {
    rewrite "^/(.*)$" /es/$1;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.