Search code examples
phphtmlhref

php script includes html in different directory


I have a form handler which is written in PHP and resides in a different directory than the html files. When the handler runs, it needs to include one of the html files. The html files have relative hrefs in them, which break because the page was served from the PHP directory, not the html directory.

For example, index.html contains

<link rel="stylesheet" type="text/css" href="css/site_global.css?4013920463"/>

These links are produced by Adobe Muse and expect that "css" is a subdirectory under the location of the html files and that the page was served from the html directory. Again, since I'm serving the page from the PHP directory, the relative links break.

Short of putting in absolute paths for the hrefs, is there any other technique I should consider? I really don't want to put in absolute paths because they will break for other reasons.

Ideally, I'd like to use some sort of method that allows me to set the "working path" in the browser - so that I can tell it to fetch hrefs from the right place.


Solution

  • Relative paths in a browser are computed based on the current page path (see here). If you are looking at http://foo.bar/one/page.html , the site_global.css path will be http://foo.bar/one/css/site_global.css .

    If I understood your question, you can use the element to set a base URL for all the relative links in the page.

    See here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base