Reputation: 1647
If I have two folders in localhost:
How can I make all relative urls in files within each folder use the respective folder as the root? If possible, is there a general way to have all folders in the sites folder automatically serve as the root for all relative urls in them?
Upvotes: 1
Views: 857
Reputation: 11540
The easiest way to achieve what you are asking is by using the HTML base tag (although there are some drawbacks). This allows you to specify a default URL for all links within a site. In the following example the relative root for the document is http://www.apache.org/ so the /images/feather-small.gif would be loaded from http://www.apache.org/images/feather-small.gif.
<html>
<head>
<title></title>
<base href="http://www.apache.org/" />
</head>
<body>
<img src="/images/feather-small.gif" />
</body>
</html>
For your sites you would need each site to contain slightly different base tags like:
<base href="http://localhost/sites/cats/" />
<base href="http://localhost/sites/dogs/" />
I would imagine if you had lots of sites this could get pretty tedious (especially if you wanted to swap the sites about). One option is to configure the base tag for each site in one place and then incorporate this using something like server side includes (mod_include). Assuming your sites are static content hosted on Apache you could use mod_include could to provide the dynamic base tag includes. Alternatively if your sites were dynamically scripting using PHP, ASP, JSP (or similar) you could acquire or generate the base tag using those technologies.
If you created a basetag.txt containing:
<base href="http://localhost/sites/cats/" />
you could reference this in your static content, e.g. index.html might contain:
<!--#include virtual="./basetag.txt" -->
Then if you needed to move the site to say "felines" you would only need to edit the one file basetag.txt.
I hope this helps.
Upvotes: 3