Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to convert the following Apache mod_rewrite rule into an appropriate nginx format:

RewriteCond %{REQUEST_FILENAME} (.*)/([^\/])([^\/]+)/?$
RewriteCond %1/%2/%2%3 -d
RewriteRule ^/?.* /%2/%2%3 [L] 

In practice, this rule rewrites bare directory requests to place them within a parent directory based upon the first character (case-sensitive), so becomes and so on.

Everything else is either static content, or PHP and passed on with FastCGI, and there are locationhandlers in place for them.

share|improve this question

Well, you'll lose the directory test in your second RewriteCond, but:

rewrite ^/([^/])([^/]+)/?$ /$1/$1$2 last;
share|improve this answer
    location ~ /([^/])([^/]+)/?$ {
        set $firstletter $1;
        set $sub $2;
        if (-d $request_filename) {
            rewrite ^ /$firstletter/$firstletter$sub last;
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.