Search code examples
htmlabsoluteradix

Setting URL base to be able to use absolute links


I am constantly finding myself a situation where I work on my clients' projects locally and put projects in appropriately-named folders. However, on production the files are put into root. I use absolute paths starting with a forward-slash to access all assets as such:

<img src="/images/picture.jpg" />

This works fine in production environment, but in development this translates to the root folder where all my projects are, so I would naturally have to include the folder name in the source above, which really breaks the flow of work:

<img src="/project/images/picture.jpg" />

I have read up about RewriteBase, but that seems to address htaccess rewrite rules only. There is also a tag that could go into the HEAD section of the doc, but that does not seem to work too well with the front-coming forward slash. I would prefer to keep that forward-slash. Also, might be important to note, that my CSS files follow the same path logic.

Does anyone have a solution to my issue? Thanks.


Solution

  • As I've understood your question you are accessing your projects like http://localhost/project1/, http://localhost/project2/, ...

    Create a virtual host for every project.

    If your project is www.example.com you would create a virtual host (on your development machine) local.example.com which resolves to 127.0.0.1.