title: Custom URL routing with PHP and regex
body: <p>I'm trying to create a very simple URL routing, and my thought process was this:</p>
  
  <ul>
  <li>First check all static URLs</li>
  <li>Then check database URLs</li>
  <li>Then return 404 if neither exists</li>
  </ul>
  
  <p>The static URLs are easy to do of course, but I'm trying to figure out the best way to do dynamic ones.  I would prefer not to have to set a static prefix, despite knowing that it would make this a lot easier to code.</p>
  
  <p>This is what I currently have:</p>
  
  <pre><code>$requestURL = $_SERVER['REQUEST_URI'];
  
  if ($requestURL == '/') {
      // do stuff for the homepage
  }
  
  elseif ($requestURL == '/register') {
      // do stuff for registration
  }
  
  // should match just "/some-unique-url-here"
  elseif (preg_match("/([\/A-Za-z0-9\-]+)/",$requestURL)) { 
      // query database for that url variable
  }
  
  // should match "/some-unique-url/and-another-unique-url"
  elseif (preg_match("(^\/[A-Za-z0-9\-]+\/[A-Za-z0-9\-]+)/",$requestURL)) {
      // query database for first and second variable
  }
  
  else {
      // 404 stuff
  }
  </code></pre>
  
  <p>My problem is that if I have "/register" URI, it will match the second elseif statement as well as the regex statement.  But I want to avoid having to specifically exclude each static URL from regex statement, such as this:</p>
  
  <pre><code>// should match just "/some-unique-url-here"
  elseif ((preg_match("/([\/A-Za-z0-9\-]+)/",$requestURL)) &amp;&amp;
      ($requestURL !== '/register') &amp;&amp;
      ($requestURL !== '/')) { 
      // query database for that url variable
  }
  </code></pre>
  
  <p>What's the easiest way to solve this problem?  I'll probably have like 15-20 static URLs, so specifically excluding all of them would be very clunky.</p>