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

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 http://example.com/foobar becomes http://example.com/f/foobar 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
add comment

2 Answers 2

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

rewrite ^/([^/])([^/]+)/?$ /$1/$1$2 last;
share|improve this answer
add comment
    location ~ /([^/])([^/]+)/?$ {
        set $firstletter $1;
        set $sub $2;
        if (-d $request_filename) {
            rewrite ^ /$firstletter/$firstletter$sub last;
share|improve this answer
add comment

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.