如何在静态站点中创建根相对链接?

sea*_*ter 11 html url relative-path root middleman

构建静态HTML网站时,您可以像这样设置基本网址<base url="http://localhost:8888/mysite" />.据说当你插入一个图像时,你可以从那个基本网址中这样做<img src="/img/logo.png" />,这相当于<img src="http://localhost:8888/mysite/img/logo.png" />

我的问题是,当我移动网站时,这些相对链接不起作用,这很痛苦,因为我试图与Dropbox上的某人分享.我以为我可以将基本网址添加到其中<base url="http://dl.dropbox.com/u/xxxxxxxx/mysite" />,但图片链接在这里查看:<img src="http://dl.dropbox.com/img/logo.png" />而不是我在头部设置的完整基本网址.

为什么是这样?

tob*_*ies 16

丢失前导/使其成为相对URL:

<img src="img/logo.png" />
Run Code Online (Sandbox Code Playgroud)

有3种类型的URL:

  • 完全合格,例如 http://example.org/path/to/file

  • 绝对,例如/path/to/file(假设链接来自example.org域中的任何页面)

  • 相对,例如path/to/file(假设链接来自root(/)"文件夹"或存在基本URL http://example.org/)或to/file(假设链接来自'path'"文件夹"或基本URL http://example.org/path/)

  • 我总是听到一个带有斜杠(`/`)的网址,称为_root relative urls_.另外,还有_protocol relative_(有时称为scheme relative)url:`<script src ="// domain.com/... (3认同)