I would like to know the differences between these two types of URLs: relative URLs (for pictures, CSS files, JS files, etc.) and absolute URLs.
In addition, which o
There are really three types that should be discussed explicitly. In practice though URLs have been abstracted to be handled at a lower level and I would go as far as to say that developers could go through their entire lives without writing a single URL by hand.
Absolute URLs tie your code to the protocol and domain. This can be overcome with dynamic URLs.
https://dev.example.com/a.html?q=
Absolute Pros:
Control - The subdomain and protocol can be controlled. People that enter through an obscure subdomain will be funneled into the proper subdomain. You can hop back and forth between secure and non-secure as appropriate.
Configurable - Developers love things to be absolute. You can design neat algorithms when using absolute URLs. URLs can be made configurable so that a URL can be updated site-wide with a single change in a single configuration file.
Clairvoyance - You can search for the people scraping your site or maybe pick up some extra external links.
Root Relative URLs tie your code to the base url. This can be overcome with dynamic URLs and/or base tags.
.example.com/index.php?q=
Root Relative Pros:
Relative URLs tie your code to the directory structure. There is no way to overcome this. Relative URLs are only useful in file systems for traversing directories or as a shortcut for a menial task.
index.php?q=
Relative Cons:
CONFUSING - How many dots is that? how many folders is that? Where is the file? Why isn't it working?
MAINTENANCE - If a file is accidentally moved resources quit loading, links send the user to the wrong pages, form data might be sent to the incorrect page. If a file NEEDS to be moved all the resources that are going to quit loading and all the links that are going to be incorrect need to be updated.
DOES NOT SCALE - When webpages become more complex and views start getting reused across multiple pages the relative links will be relative to the file that they were included into. If you have a navigation snippet of HTML that is going to be on every page then relative will be relative to a lot of different places. The first thing people realize when they start creating a template is that they need a way to manage the URLs.
COMPUTED - They are implemented by your browser (hopefully according to RFC). See chapter 5 in RFC3986.
OOPS! - Errors or typos can result in spider traps.
Developers have stopped writing URLs in the sense being discussed here. All requests are for a website's index file and contain a query string, aka a route. The route can be thought of as a mini URL that tells your application the content to be generated.
http://dev.example.com/index.php/my:whacky:url
Routes Pros:
Most people will make use of all three forms in their projects in some way or another. The key is to understand them and to choose the one best suited for the task.