1

I have seen this question, but that is answering something else.

Today, I got a strange (at least to me) problem. The problem is, that I have this code in my website...

<link rel="icon" type="image/png" sizes="16x16" href="~/img/TT_favicon_16px.png" />

The favicon was returned on almost all of the pages of the website, but not on one of the pages of the website.

I just imagined (out of nothing) to remove the tilde sign and it started working on all of the pages on the website.

<link rel="icon" type="image/png" sizes="16x16" href="/img/TT_favicon_16px.png" />

This raised my curiosity and I wanted to understand what is the difference between the two...

Naveed Butt
  • 2,652
  • 4
  • 26
  • 50
  • Do you use any meta base tags in your HTML of the pages where the favicon doesn't work, and where it does work ? – nl-x Sep 07 '18 at 07:50

1 Answers1

1

It is contextual.

Let's say the current page's URL is http://example.com/foo/bar.html.

Absolute path links, starting with a slash, like /quux/grault.html, will replace the entire path, leaving the server alone: http://example.com/quux/grault.html.

Relative path links, not starting with a slash, like fred/baz.html, will append to the last slash of the path: http://example.com/foo/fred/baz.html.

Where HTTP and its interpretation is concerned, your tilde is nothing special. It is not a full URL, and it does not start with a slash, therefore it is a relative path URL, and within our example you'd end up with this URL: http://example.com/foo/~/img/TT_favicon_16px.png. When you remove the tilde, it becomes an absolute path link, and you get http://example.com/~/img/TT_favicon_16px.png.

When you visit those two links, what gets served depends on your web server configuration. I've never tried a link like http://example.com/~/... or http://example.com/foo/~/..., but I doubt it would work - most web servers are not configured to respond with anything sensible on such a URL (and indeed on my computer I get a 404 Not Found). Why you got a response there is impossible to say without inspecting how your web server is configured.

Amadan
  • 169,219
  • 18
  • 195
  • 256
  • Thanks for the explanation, but what I can't understand is the resolving of the tilde sign in the `` tag. I know that .Net translates that to the root of the application, but with tag, how does it translates or if it is Visual Studio or .Net itself that does some magic here ? – Naveed Butt Sep 07 '18 at 07:33
  • "When you remove the tilde, it becomes an absolute path link, and you get `http://example.com/~/img/TT_favicon_16px.png`." ... Actually if he removes the tilde, he does not get a url with still a tilde in it. He simply gets `http://example.com/img/TT_favicon_16px.png` – nl-x Sep 07 '18 at 07:44