Absolute URL from base folder with rewrite
<p>I have a project running local on WampServer. It's an MVC-like structure; it rewrites the URL to <code>index.php?url=$1</code>. Full <code>.htaccess</code>:</p>

<pre><code>RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
</code></pre>

<p>When I want to send the user to another page using PHP <code>location: &lt;location&gt;</code> it doesn't do this properly because of the rewriting (all-though I am technically always in <code>index.php</code>).</p>

<p>For example if I am on <code>http://localhost/project_name/controller/method/</code> and this controller's constructor or method tries to send me to:</p>

<ul>
<li><code>header('location: another_controller/method');</code> sends me to
<code>http://localhost/project_name/controller/method/another_controller/method/</code></li>
<li><code>header('location: /another_controller/method');</code> sends me to
<code>http://localhost/another_controller/method/</code></li>
</ul>

<p>But I want it to send me like this:</p>

<ul>
<li><code>header('location: /another_controller/method');</code> sends me to
<code>http://localhost/project_name/another_controller/method/</code></li>
</ul>

<p>Now the only solution I have found is:</p>

<pre><code>define('BASE_URL','http://localhost/project_name');
header('location: '.BASE_URL.'/another_controller/method/');
</code></pre>

<p>But this isn't perfect either because it causes me to have to change this defined constant <code>BASE_URL</code> whenever the domain or folder name changes. I could also create a method in my <code>BaseController</code> that creates absolute URLs, but this method would basically just prepend <code>BASE_URL</code> too.</p>

<p><strong>Note:</strong> The same problem doesn't arise with HTML's <code>src</code> and <code>href</code> attributes, which can use relative paths (without <code>project_name</code> folder in path). I don't understand why however. Because if the header <code>location</code> causes the browser to append the the relative-URL to the current location, why doesn't it have the same behavior when looking for <code>.css</code> or <code>.js</code> files.</p>

<p><strong>So...</strong> this raises a couple of questions for me:</p>

<ol>
<li>Would I have this problem if I had virtual hosts?</li>
<li>What is the best way to solve this problem?</li>
<li>Is is best to just have the full absolute URL?</li>
<li>Why do HTML's <code>src</code> and <code>href</code> attributes not share this behavior?</li>
</ol>