如何在静态站点中创建相对于根的链接? [英] How do I create root-relative links in a static site?
问题描述
在构建静态HTML网站时,您可以像< base url ="http://localhost:8888/mysite"/>
那样设置基本URL.假设当您插入图片时,您可以通过该基本网址执行操作,例如< img src ="/img/logo.png"/>
,它等效于< img src ="http://localhost:8888/mysite/img/logo.png"/>
When building a static HTML site, you can set the base url like so <base url="http://localhost:8888/mysite" />
. Supposedly when you insert, say, an image, you can do so from that base url like so <img src="/img/logo.png" />
, which is equivalent to <img src="http://localhost:8888/mysite/img/logo.png" />
我的问题是,当我移动网站时,这些相对链接不起作用,这很痛苦,因为我试图与Dropbox上的某人共享它.我以为我可以将基本URL替换为< base url ="http://dl.dropbox.com/u/xxxxxxxx/mysite"/>
,但是图像链接在此处:< img src ="http://dl.dropbox.com/img/logo.png"/>
,而不是我在标题中设置的完整基本URL.
My problem is that these relative links don't work when I move the site around, which is a pain because I'm trying to share it with someone on Dropbox. I thought I could just chage the base url to <base url="http://dl.dropbox.com/u/xxxxxxxx/mysite" />
, but the image links are looking here: <img src="http://dl.dropbox.com/img/logo.png" />
instead of the full base URL I set in the head.
这是为什么?
推荐答案
丢失前导/
使其成为相对URL:
Lose the leading /
to make it a relative URL:
<img src="img/logo.png" />
URL有3种类型:
-
完全合格,例如
http://example.org/path/to/file
绝对值,例如/path/to/file
(假设链接来自example.org域中的任何页面)
Absolute, e.g. /path/to/file
(assuming the link comes from any page in the example.org domain)
相对,例如 path/to/file
(假设链接来自根目录("/
")文件夹",或者有基本URL http://example.org/
)或 to/file
(假设链接来自路径"文件夹"内,或者基本URL为 http://example.org/path/
))
Relative, e.g. path/to/file
(assuming the link comes from the root (/
) "folder" or there is a base URL http://example.org/
)
or to/file
(assuming the link comes from within the 'path' "folder" or the base URL is http://example.org/path/
)
这篇关于如何在静态站点中创建相对于根的链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!