简体   繁体   English

参考.css 和脚本生成的图像 HTML

[英]referring to .css and images from script-generated HTML

I have a site with static HTML pages in the home directory.我在主目录中有一个带有 static HTML 页面的站点。 These HTML pages use relative paths to refer to images, css, and links ie这些 HTML 页面使用相对路径来引用图像、css 和链接,即

<img src="images/myimg.gif">

and

<a href="contactUs.html">Contact Us</a>

I also have a monolithic script whose URL is, ie http://mysite.com/myScript which uses "extra path info" to select functions... ie http://mysite.com/myScript/products shows a list of products. I also have a monolithic script whose URL is, ie http://mysite.com/myScript which uses "extra path info" to select functions... ie http://mysite.com/myScript/products shows a list of products . So in HTML generated from the script I need to refer to images, css and links like this:因此,在从脚本生成的 HTML 中,我需要参考图像、css 和如下链接:

<img src="../images/myimg.gif">

and

<a href="../contactUs.html">Contact Us</a>

The problem is now I want to start moving common HTML into include files, (eg common header and footer), but the fact that the script and the static HTML refer to relative resources in different ways is complicating matters. The problem is now I want to start moving common HTML into include files, (eg common header and footer), but the fact that the script and the static HTML refer to relative resources in different ways is complicating matters.

I don't want to use absolute paths because that messes up my colleague's work when she tries to work on the pages in DramWeaver, and it also makes the site less flexible.我不想使用绝对路径,因为当我的同事尝试在 DramWeaver 中处理页面时,这会扰乱她的工作,而且这也会降低站点的灵活性。

What's the best way to solve this issue?解决此问题的最佳方法是什么? One idea I had was to use URL rewriting in Apache to allow the URL to http://mysite.com/products to really use http://mysite.com/myScript/products but I don't have experience with URL rewriting so I don't know how easy that would be. One idea I had was to use URL rewriting in Apache to allow the URL to http://mysite.com/products to really use http://mysite.com/myScript/products but I don't have experience with URL rewriting so我不知道这有多容易。 Another idea I had was to use the META BASE attribute in HTML but I don't like the fact that I would have to hard-code that into every HTML page, and it would have to have the full URL hard-coded (eg http://mysite.com/ ) into each one. Another idea I had was to use the META BASE attribute in HTML but I don't like the fact that I would have to hard-code that into every HTML page, and it would have to have the full URL hard-coded (eg http://mysite.com/ )进入每一个。 Any advice?有什么建议吗?

Can't you refer to your images with a slash at the beginning so all files linked to are from the root, no matter how deep you are in the directory structure you are?您不能在开头使用斜杠引用您的图像,以便所有链接到的文件都来自根目录,无论您在目录结构中有多深? Eg:例如:

<img src="/images/myimg.gif" />

EDIT:编辑:

You could use $_SERVER to get the path then use substr_count to count the number of slashes in the path.您可以使用$_SERVER获取路径,然后使用substr_count计算路径中的斜杠数。 Add as many ../ 's as you need based on that number.根据该数字添加尽可能多的../ Would that work for you?这对你有用吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM