Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to keep a website with url routing directory independent
    primarykey
    data
    text
    <p>I'm developing a PHP website that uses url routing. I'd like the site to be directory independent, so that it could be moved from <a href="http://site.example.com/" rel="noreferrer">http://site.example.com/</a> to <a href="http://example.com/site/" rel="noreferrer">http://example.com/site/</a> without having to change every path in the HTML. The problem comes up when I'm linking to files which are not subject to routing, like css files, images and so on. </p> <p>For example, let's assume that the view for the action <code>index</code> of the controller <code>welcome</code> contains the image <code>img/banner.jpg</code>. If the page is requested with the url <a href="http://site.example.com/welcome" rel="noreferrer">http://site.example.com/welcome</a>, the browser will request the image as <a href="http://site.example.com/img/banner.jpg" rel="noreferrer">http://site.example.com/img/banner.jpg</a>, which is perfectly fine. But if the page is requested with the url <a href="http://site.example.com/welcome/index" rel="noreferrer">http://site.example.com/welcome/index</a>, the browser will think that <code>welcome</code> is a directory and will try to fetch the image as <a href="http://site.example.com/welcome/img/banner.jpg" rel="noreferrer">http://site.example.com/welcome/img/banner.jpg</a>, which is obviously wrong.</p> <p>I've already considered some options, but they all seem imperfect to me:</p> <ul> <li><p>Use url rewriting to redirect requests from (<strong>*.css</strong>|<strong>*.js</strong>|...) or (<strong>css/*</strong>|<strong>js/*</strong>|...) to the right path. </p> <p><em>Problems</em>: Every extension would have to be named in the rewrite rules. If someone would add a new filetype (e.g. an mp3 file), it wouldn't be rewritten.</p></li> <li><p>Prepend the base path to each relative path with a php function. For example:<br> <code>&lt;img src="&lt;?php echo url::base(); ?&gt;img/banner.jpg" /&gt;</code> </p> <p><em>Problems</em>: Looks messy; <strong>css</strong>- and <strong>js</strong>-files containing paths would have to be processed by PHP.</p></li> </ul> <p>So, how do you keep a website directory independent? Is there a better/cleaner way than the ones I came up with?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload