title: Extract directory from REQUEST_URI
body: <p>I'm interested in extracting the directory from <code>$_SERVER['REQUEST_URI']</code>. I believe I specifically need to use <code>$_SERVER['REQUEST_URI']</code> in case the URL is being <code>mod_rewrite</code>'d.</p>
  
  <p>If <code>$_SERVER['REQUEST_URI'] = 'http://example.com/path/to/resource';</code>, then I can just use <code>dirname()</code> on it and get <code>http://example.com/path/to</code>.</p>
  
  <p>If the URI is the directory index page/equivalent, but a query string or hash is appended to it, then <code>$_SERVER['REQUEST_URI']</code> might be something like <code>'http://example.com/path/?query'</code> or <code>http://example.com/path/#hash</code>. In this case, <code>dirname()</code> works as well -- both of those resolve to <code>http://example.com/path</code>.</p>
  
  <p>If, however, the URI has no query string or hash, <code>pathname()</code> strips off the bottom-level directory, e.g. <code>http://example.com/path/</code> becomes <code>http://example.com</code>. My solution was to simply append a hash symbol to every URI, i.e. <code>dirname($_SERVER['REQUEST_URI'].'#')</code>. <code>dirname('http://example.com/path/'.'#')</code> yields <code>http://example.com/path</code>.</p>
  
  <p>But I don't understand how URIs work, so I don't really know if this is safe.</p>
  
  <ul>
  <li>If a URI points to a directory, does it <em>always</em> end in the character <code>/</code>? If I type <code>http://example.com/path</code> in my browser, it redirects to <code>http://example.com/path/</code>, adding the trailing slash. Will this <em>always</em> happen, though?</li>
  <li>Do query strings and hashes <em>always</em> get URL-encoded? For example, I can type <code>http://example.com/path/?query=a/b/c</code> in my browser and it gets interpreted as <code>http://example.com/path/?query=a%2Bb%2Bc</code>. But if that conversion didn't happen, then <code>pathname()</code> would return <code>http://example.com/path/?query=a/b</code>, which would be bad.</li>
  <li>Can URIs have <code>\</code>s or are directories always separated with <code>/</code>?</li>
  </ul>