Q:

Multiple conditions for a Nginx rewrite

I'm trying to find the best way to rewrite this rule so it will work on nginx:
   RewriteCond %{REQUEST_URI} !^/clip-art/.*$
   RewriteCond %{REQUEST_URI} !^/Clip-Art/.*$
   RewriteCond %{REQUEST_URI} !^/cgi-bin/.*$
   RewriteCond %{REQUEST_URI} !^/images/.*$
   RewriteCond %{REQUEST_URI} !/invitation/
   RewriteCond %{REQUEST_URI} !/designer/
   RewriteRule ^/(.*)/(.*)/(.*)/(.*)$ /clip-art/$1/$2/$3/$4 [r=301,nc,l]

I know I can do something like:
   if ($request_uri !~ "/designer") {
       rewrite ^/(.*)/(.*)/(.*)/(.*)$ /clip-art/$1/$2/$3/$4 permanent;
   }

But obviously that only works with one of the folder names. How do I make it work with multiple folder names?

A:

Regular expressions are relatively inefficient and generally discouraged in Nginx configurations when other methods would suffice.  There are a few cases here that don't require regexes:
location /clip-art/ {
    # ...
}

location /Clip-Art/ {
    # ...
}

location /images/ {
    # ...
}

location /cgi-bin/ {
    # ...
}

If you're just serving static images from at least three of those, we can make it even simpler.  The ^~ prefix means that these rules will take precedence over regex-based rules, which would otherwise take precedence.  While this has many uses, it can act as a security feature when dealing with directories containing only static resources.  If someone manages to upload a PHP file or some other executable code that would normally be handled by a regex location, the pre-regex location (with ^~) will win, and the file won't be executed.  I've also omitted the typical $uri/ because it's not really applicable to static resource directories.
location ^~ /clip-art/ { try_files $uri =404; }
location ^~ /Clip-Art/ { try_files $uri =404; }
location ^~ /images/   { try_files $uri =404; }

location /cgi-bin/ {
    # ...
}

As for the remaining conditions, Nginx is designed to match starting from the beginning of each URI.  Ideally, you can handle these sorts of scenarios in your application, rather than within Nginx.  However, in this specific instance, a relatively efficient regex can be written:
rewrite "^((?:/(?!designer|invitation)[^/]+){4})/?$" /clip-art$1 permanent;

