ok so i am including a html file that has a css file within it. but when i include it, it breaks the relative css location. is there a way before i include the html file that i define the relative area.

example
include("admin/template");

html file
<link href="css/style.css" rel="stylesheet" type="text/css" />

the reason that i want to see if i can do it before is so that i dont have to edit the html file for it to be the correct relative. just upload it and go.

thanks in advance

Dani AI

Generated

Brief diagnosis: relative resource URLs inside an included HTML fragment are resolved by the browser against the URL of the page that was requested (the including page), not the filesystem location of the included file. That is why a template that uses relative links to its CSS/JS breaks when injected from a different directory. The desire from to avoid editing uploaded templates is common and solvable.

Quick, non-invasive fix: set a base URL in the host page’s head before the template is inserted. For example:

<base href="/admin/">

Placing that before any relative href/src makes those references resolve as if the document were served from /admin/. Caveats: only the first <base> applies, it affects all relative links (scripts, forms, anchors), and it must appear before the template’s relative elements.

If a global base is too blunt, capture and rewrite the included HTML on the server to fix resource paths. Example pattern:

ob_start();
include '/path/to/template.html';
$html = ob_get_clean();
$html = preg_replace('#(href|src)=([\'"])(?!/|https?://)([^\'"]+)\\2#i', '$1=$2/admin/$3$2', $html);
echo $html;

Adjust the regex to target only CSS/JS/image paths you need. This also allows handling CSS-internal URLs (background images) by parsing and rewriting url(...) values.

Extra notes: prefer root‑relative or absolute asset URLs if possible, since they are the simplest long-term solution. Server-side chdir() or similar does not change how the browser resolves relative links. ’s point about path resolution is on target, and ’s per-file include approach still needs correct asset paths or one of the fixes above.

Recommended Answers

All 3 Replies

Why can't you just define the css file like normal? And if your file's name is template, the include is looking for the css file in admin/css/

im just trying to make it so you upload it as is and then its ready to go. so if you go back to editing it you dont have to change all the css or javascript urls. specially if there are lot of files.

i would like the code to take care of it

you put your template files one by one...like

include "admin/template/sample.dwt";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.