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" />
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.
Why is this?
To link pages using relative URL in HTML, use the <a> tag with href attribute. Relative URL is used to add a link to a page on the website. For example, /contact, /about_team, etc.
Site root–relative paths describe the path from the site's root folder to a document. You may want to use these paths if you are working on a large website that uses several servers, or one server that hosts several sites.
Yes, prefacing the URL, in the href or src attributes, with a / will make the path relative to the root directory. For example, given the html page at www.example.com/fruits/apples.html , the a of href="/vegetables/carrots. html" will link to the page www.example.com/vegetables/carrots.html .
Root-relative hyperlinks are a subset of relative hyperlinks in which all the links are assumed to start from the root folder (domain name) of the site. They differ from the relative hyperlinks in that the address is prefixed by a forward slash (for example, /default.
Lose the leading /
to make it a relative URL:
<img src="img/logo.png" />
There are 3 types of URL:
Fully Qualified, e.g. http://example.org/path/to/file
Absolute, e.g. /path/to/file
(assuming the link comes from any page in the example.org domain)
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/
)
I'm aware that I'm a little late to the game on this one, but you should really be using Rails asset tags instead of raw HTML here.
For instance, instead of using:
<img src="/img/logo.png" />
You should use:
<%= image_tag 'logo.png' %>
Assuming that:
/img/
in your config.rb fileAlternately, you could reference CSS with:
<%= stylesheet_link_tag 'file.css' %>
Javascript files can be included with:
<%= javascript_include_tag 'file.js' %>
Since Middleman allows you to control whether or not assets are referenced relatively (by uncommenting some lines in config.rb), using Rails asset tags make much more sense than static HTML ones. I highly recommend switching if you haven't already done so. If you have any further questions about ay of these tags or the ERB syntax, feel free to ask away on here!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With