Absolute Path for Deployment in a Non-Root Locatio

2019-02-15 12:47发布

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/.

8条回答
萌系小妹纸
2楼-- · 2019-02-15 13:34

what about using html's BASE element? http://www.w3.org/TR/html4/struct/links.html#h-12.4

although i´m not sure how you could have it in a single html file and inherit it to the rest of your htmls, so your source remains intact. if your site is html-only maybe with frames, otherwise you could use some sort of server-side include depending on what youre using (asp, jsp, whatever). Check out this link for more information http://www.boutell.com/newfaq/creating/include.html

查看更多
劳资没心,怎么记你
3楼-- · 2019-02-15 13:36

EDIT:

Of course it would work in case you do with php. Adding:

<? define(_BASE_, substr($_SERVER['SERVER_NAME'] . "/" . $_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], "/" )) ); ?>

<html>

    <head>
        <script src="<?=_BASE_?>/path_relative_to_project_root"></script>
    </head>

    <body>
        <img src="<?=_BASE_?>/path_relative_to_project_root">
    </body>

</html>
查看更多
登录 后发表回答