title: Complex regex in Apache rewrite rule
body: <p>I am trying to create the following behavior with a rewrite rule.</p>
  
  <pre><code>https://domain.com/admin/user/add      =&gt; /addedit.php?add=
  https://domain.com/admin/user/add/     =&gt; /addedit.php?add=
  https://domain.com/admin/user/add/123  =&gt; /addedit.php?add=123
  https://domain.com/admin/user/add/123/ =&gt; /addedit.php?add=123
  </code></pre>
  
  <ul>
  <li><code>user</code> is variable</li>
  <li><code>add</code> can either be "add" or "edit"</li>
  <li>Trailing slashes are always allowed</li>
  <li>There can be more after the slash after (add|edit) which is assigned to the variable</li>
  </ul>
  
  <p>I'm fine up until the optional slash after the action word (add|edit). That's where I don't know regex well enough to accomplish my goal.</p>
  
  <p>Something like the following works:</p>
  
  <pre><code>RewriteRule ^admin/(.*)/(add|edit)(/(.*)/?)?$ /admin/$1/addedit.php?$2=$3
  </code></pre>
  
  <p>However, the URLs end up containing the slashes in the <code>GET</code> variable. I figured I would be able to reference the <code>(.*)</code> in middle of <code>(/(.*)/?)?</code> as <code>$4</code>, but I am not.</p>
  
  <p>I then read about <a href="http://www.regular-expressions.info/brackets.html" rel="nofollow">non capturing groups</a> and so added a <code>?:</code> like so:</p>
  
  <pre><code>RewriteRule ^admin/(.*)/(add|edit)(?:/(.*)/?)?$ /admin/$1/addedit.php?$2=$3
  </code></pre>
  
  <p>It's probably a relatively simple thing I'm overlooking.</p>