Relative URLs and Absolute URLs
by kirupa (https://www.kirupa.com/me/index.htm) | 2 September 2013
A lightly contentious issue for many web developers revolves around what kind of URL scheme one should use when referencing resources and links within the same site. For example, you can use a fully-qualified absolute URL:
You can also use the more compact relative URL:
Now, this article isn't about picking sides and telling you why one is superior to the other. The reason is that you need to use both absolute and relative URLs as part of creating your sites and apps. Instead, this article will show you when to use which.
Onwards!
When to Use Absolute URLs
Absolute URLs point to something without any regard for context. A URL like //www.kirupa.com/images/foo.png is something that works in your browser, on a friend's blog, in outer space, or wherever. There aren't too many times when you would need to use absolute URLs. In fact, I can think of two general cases when you need to...
You are Pointing to Content Outside of Your Site
If the content you are referencing lives outside of your domain, you have to use an absolute URL:
[Image: absolute references] (https://www.kirupa.com/html5/images/absolute_references.png)
There is simply no way to use relative URLs for things that go outside of the domain your HTML page is currently in. This applies to things in subdomains as well. You can't reference something in a subdomain using a relative URL syntax.
Your References Will Change Depending on Context
Some of your HTML will find itself merged with other pieces of HTML and served on a variety of different pages in your site. While this may sound like a bizarre scenario, this is exactly what happens when you employ server side includes or site-wide templates to make your content more modular and reusable.
URLs inside these "template" pages cannot assume that relative paths will still work when they find themselves in different locations. For example, this is what the markup for something on this site that gets server side included looks like: