6.S063 Design for the Web: Languages and User Interfaces

URLs

What is a URL?

In technical terms, A URL (Uniform Resource Locator) is a string of characters that defines the address of a web page or other resource on the internet. It has a few parts we should know about:

When you enter a URL into your web browser, the browser sends a request to the server specified by the domain name. The server then sends back the resource (e.g. a web page) that corresponds to the URL and your browser displays the response (usually HTML, and some CSS/JS files) on your screen.

There are two main types of URLs we are interested in: absolute and relative URLs.

Absolute URLs

Absolute URL is the address that includes all of above, i.e., the full address. The absolute URL of this webpage is https://designftw.mit.edu/resources/urls/index.html. When you want to share this webpage with a friend, then you send the absolute URL, so they can access to the exact same resource you are accessing. However, you should never use absolute URLs when coding a website unless it's a pointing to another website. To see why, we can look at an imaginary but a possible scenario. Let's say we used absolute URL to link this page from the resources list:

...
<li><a href="https://designftw.mit.edu/resources/urls/">URLs</a></li>
...

Not only this, but we used absolute URLs everywhere on this website. Everything is working as expected until it turns out the weather conditions broke MIT's all nameservers in addition to the Stratton Student Center (aka Stud), and designftw.mit.edu is not accessible anymore. We still want you to access the course website, so we forward you to our alternate the domain designftw.netlify.app. But there's an issue. When you click on any link, it goes back to designftw.mit.edu 😯 😯 😯

This extreme example illustrates the maintainability issues that arise from using absolute URLs: many links would need to be edited. In addition, the repetition of https://designftw.mit.edu/... everywhere clutters the appearance of the links. It is also important to consider that when developing locally, a domain is typically not available yet. In this case, how would you link to resources? Using http://localhost/... everywhere only returns to the original issue.

Fortunately, there's a solution!

Relative URLs

Relative URLs are used to specify the location of a resource in relation to the current page, rather than specifying the absolute URL. They are typically shorter and easier to read than absolute URLs, and they are often used when the resource is within the same domain as the current page.

There are two types of relative URLs we'll use the most.

This process might remind you something you use everyday: the file explorer of your computer. That's because websites are mostly also just a tree of files and directories. Here's how this this website might look like (not actually, but given as example):

. (root=designftw.mit.edu)
├── index.html (home page)
├── resources
│   ├── index.html (resources list page)
│   ├── urls
│   │   └── index.html (URLs page, you are here)
│   ├── editors
│   │   └── index.html (editors page)
│   ├── localhost
│   │   ├── media
│   │   │   └── live_server.gif (gif for live server extension)
│   │   └── index.html (Starting a local server page)
│   ...... (other resources omitted)
│
├── schedule
│   └── index.html (schedule page)
│
├─ ...... (other resources omitted)
│
...... (other resources omitted)

So, if you are in the "/resources/localhost/" path (editing index.html), then you can access to live_server.gif with relative path "media/live_server".

Check yourself: How would you get live_server.gif from the path "/schedule/"?

Here's a small demo you can play with to experiment with relative paths. Notice that base URL being "." means the base is the current location (i.e., this page). First, try changing only the relative url to see what other paths you can explore, then also try changing base URL. For example, you could set it to "/" to see relative paths from the root of this website.

Result (absolute URL): {{new URL(relative, absoluteBase)}}

Other types of relative URLs

It's good to know that there's also query-relative URLs that will change the query string. It can be used such as:

<a href="?q=url+query+string">Search for "url query string"</a>

There's also hash-relative URLs:

<a href="#absolute-urls">Jump to Absolute URLs section</a>

Try clicking on it: Jump to Absolute URLs section. In a webpage, a hash will typically refer to the element with given id attribute. Check for yourself: inspect this page and see what's the id attribute of the "Absolute URLs" header.

You can also read about protocol-relative URLs here: Protocol-relative URL.