title: Nginx rewrite with conditional destination
body: <p>I have a site with two RESTful URL schemes:</p>
  
  <p><code>foo.com/model/id</code> and
  <code>foo.com/model/id/action</code></p>
  
  <p>The actual pages served by these URLs are in the form of</p>
  
  <p><code>$model.php?id=$id</code> and 
  <code>$model_$action.php?id=$id</code> respectively.</p>
  
  <p>I have a single regular expression that will match both cases <code>^(\w+)s/([A-z0-9]+)(?:/(\w+))?/?$</code> and I'd like to use a single Nginx rule to rewrite for both types of URLs, but I'm not sure how to do this. In pseudocode I imagine the rule would look something like this</p>
  
  <p><code>rewrite ^(\w+)s/([A-z0-9]+)(?:/(\w+))?/?$ /(($3) ? $1_$3.php?$id=$2 : $1.php?$id=2)</code></p>
  
  <p>This isn't valid syntax (so far as I know), but can something like this be done in Nginx?</p>