I wonder if this is possible to tell via .htaccess or any other manners to set a base URL.
Example
My CSS in the index.php is set like this
<link rel="stylesheet" href="css/style.css"/>
Everything works fine when i'm on the first directory to load my file and pages. But when I have a page set in a folder like /page/index.html. The css isn't loaded because the folder is not in page/ but in the root directory.
So is that possible to tell it to look always from the root directory to load the CSS, images, javascript, etc or I have to set the full url path to each element I want to load (CSS, images, javascript, etc)?
Thanks
Use Base URL, for example:-
<html>
<head>
<base href="http://www.w3fools.com/" target="_blank" />
<!-- only one base element is allowed -->
<link rel="stylesheet" type="text/css" href="/stdtheme.css" />
</head>
<body>
<img src="stickman.gif" width="24" height="39" /> - Notice that we have only specified a relative address for the image. Since we have specified a base URL in the head section, the browser will look for the image at "http://www.w3schools.com/images/stickman.gif"
<br /><br />
<a href="http://www.w3fools.com">Don't use W3Schools</a> - Notice that the link opens in a new window, even if it has no target="_blank" attribute. This is because the target attribute of the base element is set to "_blank".
</body>
</html>
You should use an absolute path in your pages:
<link rel="stylesheet" href="/css/style.css"/>
Base tags can cause numerous headaches as all non-absolute URIs will be used with it.
You're looking for the html base
tag. To quote the docs:
For example, given the following BASE declaration and A declaration:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>Our Products</TITLE>
<BASE href="http://www.aviary.com/products/intro.html">
</HEAD>
<BODY>
<P>Have you seen our
<A href="../cages/birds.gif">Bird Cages</A>?
</BODY>
</HTML>
the relative URI "../cages/birds.gif"
would resolve to:
http://www.aviary.com/cages/birds.gif
What you might be looking for is the HTML <base href="your base URL here">
tag. It's the base URI of the document, all relative links are resolved to. If the tag is not within the documents source, the URI from the browser address will be used.
Instead of this you can use absolute URIs as well.
Please see Links in HTML Documents to understand what you're making use of.