I typically refer to any assets on my site using absolute path so that I don't have to worry about the location of the assets relative to current file.
<!-- Using a absolute path. -->
<img src="/images/flag.png" />
<!-- Using a relative path. -->
<img src="../../../images/flag.png" />
However, this time I need to host the site at a non-root location e.g. http://my-server.com/holiday/
.
How would I go about this? I am looking for a solution that doesn't require me to change the path in my files. How do I configure the server (Apache) to treat http://my-server.com/holiday/
as a "root"?
Clarification:
I still need http://my-server.com/
to behave "normally". That is, it should still point to http://my-server.com/index.html
i.e. doesn't get redirected to http://my-server.com/holiday/
.
Easy...
Assuming I'm understanding what you mean, this should do you just fine. Point of order, this .htaccess should be in /holiday/
I do this locally on MAMP for testing a website that's base is in
http://localhost:8888/SocialNetwork/
... If I didn't have that, my absolute paths of, say,/profile
would go tohttp://localhost:8888/profile/
instead ofhttp://localhost:8888/SocialNetwork/profile
If you have a list of filename extensions that should be redirected, you might want to use the rewrite conditions using pattern matching againt the extensions.
Another solutions is to simply create an /images directory under root and let images be downloaded from there. Or have there links to images in your path.
try to add this to your .htaccess
or take a look here - Changing the root folder via .htaccess
If you’re using Apache, there one rather simple thing you could do by just using an SSI variable in your paths. Do a global replace of all src="/ to something like
and then in your htaccess for the specific folder define the prefix variable as /holiday
For sites that don’t have the variable or SSI, it’ll just show up as a comment or you can define it as an empty string.
Of course this means you’ll have to turn on SSI in Apache.
I don't think this is gonna happen for you man, sorry.
You have absolute paths in your documents, and want apache to prepend /holiday to them all without effecting the actual docroot? You can have one or the other, but not both.
You need to either do a mass edit and prepend the directory yourself, or move your images/css/etc into the actual root directory.
mod_rewrite is powerful, but can't really determine intent and parse the same url two different ways depending on what the user wants.
Edit:
I am wrong, but I don't have your answer. You may be able to use IS_SUBREQ in mod_rewrite to only apply the re-write conditions for sub-requests from your /holiday/index.php
I hate to say this but none of the above provides the solution. The answers by @Zoltan and @stslavik were close but, unfortunately, didn't work when deployed; I wished one of them worked, though.
As a result, I resorted to using a combination of named constants and include files in PHP. More details: File Structure for a PHP Project. Note that it doesn't have to be PHP; you can use other languages that provide similar features. +1 for @margusholland whose answer led me to experiment with this solution.