title: Rewrite rules and base url
body: <p>When one uses a rewrite rule, say:</p>
  
  <pre><code>RewriteRule ^foo/(.+?)/?$ index.php?type=foo&amp;value=$1 [NC,L]
  </code></pre>
  
  <p>The client still sees this as a url <code>server.dom/foo/bar/</code> (it has no knowledge how these url's are resolved internally, which is of course reasonable).</p>
  
  <p>If the page <code>index.php</code> however generate a link (<code>&lt;a href="baz"&gt;link&lt;/a&gt;</code>), the client will interpret this as <code>server.dom/foo/bar/baz</code>. <code>index.php</code> however has in many cases no full knowledge about where the url originates from...</p>
  
  <p>A first idea was to pass the depth of the "virtual" path as a parameter. Thus the rewrite rule would be something like:</p>
  
  <pre><code>RewriteRule ^foo/(.+?)/?$ index.php?type=foo&amp;depth=2&amp;value=$1 [NC,L]
  </code></pre>
  
  <p>As a result, <code>index.php</code> should write:</p>
  
  <pre><code>&lt;base href="../../"&gt;
  </code></pre>
  
  <p>Or more formally <code>depth</code> times <code>..</code> (separated by slashes). In some cases, however, <code>.htaccess</code> has no knowledge about the depth of the "virtual" path as well. Furthermore one can note that the last slash (<code>/</code>) in the rewrite rule is optional, depending on whether the slash was written down, the depth varies. This would increase the number of <code>RewriteRules</code> significantly...</p>
  
  <p>What are good/better practices to resolve this relative links problem robustly (robust perhaps in the sense that <em>moving</em> parts of the website will not "break" the links)?</p>