mozilla

Revision 651737 of Understanding URLs and their structure

  • リビジョンの URL スラグ: Learn/Understanding_URLs
  • リビジョンのタイトル: Understanding URLs and their structure
  • リビジョンの ID: 651737
  • 作成日:
  • 作成者: Sheppy
  • 現行リビジョン いいえ
  • コメント tweak the opening sentence

このリビジョンの内容

This article discusses Uniform Resource Locators (URLs), explaining what they are and how they're structured.

Prerequisites: You need to first know how the Internet works, what a Web server is and the concepts behind links on the web.
Objective: You will learn what a URL is and how it works on the Web.

Summary

With {{Glossary("Hypertext")}} and {{Glossary("HTTP")}}, URL is one of the key concepts of the Web. It is the mechanism used by {{Glossary("Browser","browsers")}} to retrieve any published resource on the web.

URL stands for Uniform Resource Locator. A URL is nothing more than the address of a given unique resource on the Web. In theory, each valid URL points to a unique resource. Such resources can be an HTML page, a CSS document, an image, etc. In practice, there are some exceptions, the most common being a URL pointing to a resource that no longer exists or that has moved. As the resource represented by the URL and the URL itself are handled by the Web server, it is up to the owner of the web server to carefully manage that resource and its associated URL.

Active Learning

TBD

Deeper dive

Basics: anatomy of a URL

Here are some examples of URLs:

https://developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Learn/
https://developer.mozilla.org/en-US/search?q=URL

Any of those URLs can be typed into your browser's address bar to tell it to load the associated page (resource).

A URL is composed of different parts, some mandatory and others optional. Let's see the most important parts using the following URL:

http://www.example.com:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument
Protocol
http:// is the protocol. It indicates which protocol the browser must use. Usually it is the HTTP protocol or its secured version, HTTPS. The Web requires one of these two, but browsers also know how to handle other protocols such as mailto: (to open a mail client) or ftp: to handle file transfer, so don't be surprised if you see such protocols.
Domaine Name
www.example.com is the domain name. It indicates which Web server is being requested. Alternatively, it is possible to directly use an {{Glossary("IP address")}}, but because it is less convenient, it is not often used on the Web.
Port
:80 is the port. It indicates the technical "gate" used to access the resources on the web server. It is usually omitted if the web server use the standard ports of the HTTP protocol (80 for HTTP and 443 for HTTPS) to grant access to its resources. Otherwise it is mandatory.
Path to the file
/path/to/myfile.html is the path to the resource on the Web server. In the early days of the Web, a path like this represented a physical file location on the Web server. Nowadays, it is mostly an abstraction handled by Web servers without any physical reality.
Parameters
?key1=value1&key2=value2 are extra parameters provided to the Web server. Those parameters are a list of key/value pairs separated with the & symbol. The Web server can use those parameters to do extra stuff before returning the resource. Each Web server has its own rules regarding parameters, and the only reliable way to know if a specific Web server is handling parameters is by asking the Web server owner.
Anchor
#SomewhereInTheDocument is an anchor to another part of the resource itself. An anchor represents a sort of "bookmark" inside the resource, giving the browser the directions to show the content located at that "bookmarked" spot. On an HTML document, for example, the browser will scroll to the point where the anchor is defined; on a video or audio document, the browser will try to go to the time the anchor represents.

{{Note('There are some extra parts and some extra rules regarding URLs, but they are not relevant for regular users or Web developers. Don\'t worry about this, you don\'t need to know them to build and use fully functional URLs.')}}

You might think of a URL like a regular postal mail address: the protocol represents the postal service you want to use, the domain name is the city or town,  and the port is like the zip code; the path represents the building where your mail should be delivered; the parameters represent extra information such as the number of the apartment in the building; and, finally, the anchor represents the actual person to whom you've addressed your mail.

How to use URLs

Any URL can be typed right inside the browser's address bar to get to the resource behind it. But this is only the tip of the iceberg!

The {{Glossary("HTML")}} language — which will be discussed later on — makes extensive use of URLs:

  • to create links to other documents with the {{HTMLElement("a")}} tag;
  • to link a document with its related resources through various tags such as {{HTMLElement("link")}} or {{HTMLElement("script")}};
  • to display medias such as images (with the {{HTMLElement("img")}} tag), videos (with the {{HTMLElement("video")}} tag), sounds and music (with the {{HTMLElement("audio")}} tag), etc.;
  • to display other HTML documents with the {{HTMLElement("iframe")}} tag.

Other technologies, such as {{Glossary("CSS")}} or {{Glossary("JavaScript")}}, use URLs extensively, and these are really the heart of the Web.

Absolute URLs vs relative URLs

What we saw above is called an absolute URL, but there is also something called a relative URL. Let's examine what that distinction means in more detail.

The required parts of a URL depend to a great extent on the context in which the URL is used. In your browser's address bar, a URL doesn't have any context, so you must provide a full (or absolute) URL, like the ones we saw above. You don't need to include the protocol (the browser uses HTTP by default) or the port (which is only required when the targeted Web server is using some unusual port), but all the other parts of the URL are necessary.

When a URL is used within a document, such as in an HTML page,  things are a bit different. Because the browser already has the document's own URL, it can use this information to fill in the missing parts of any URL available inside that document. We can differentiate between an absolute URL and a relative URL by looking only at the path part of the URL. If the path part of the URL starts with the "/" character, the browser will fetch that resource from the top root of the server, without reference to the context given by the current document.

Let's look at some examples to make this clearer.

Examples of absolute URLs

Full URL (the same as the one we used before)
https://developer.mozilla.org/en-US/docs/Learn
Implicit protocol
//developer.mozilla.org/en-US/docs/Learn

In this case, the browser will call that URL with the same protocol as the the one used to load the document hosting that URL.

Implicit domain name
/en-US/docs/Learn

This is the most common use case for an absolute URL within an HTML document. The browser will use the same protocol and the same domain name as the one used to load the document hosting that URL. Note: it isn't possible to omit the domain name without omitting the protocol as well.

Examples of relative URLs

To better understand the following examples, let's assume that the URLs are called from within the document located at the following URL: https://developer.mozilla.org/en-US/docs/Learn

Sub-resources
Skills/Infrastructure/Understanding_URLs
Because that URL does not start with /, the browser will attempt to find the document in a sub-directory of the one containing the current resource. So in this example,  we really want to reach this URL: https://developer.mozilla.org/en-US/docs/Learn/Skills/Infrastructure/Understanding_URLs
Going back in the directory tree
../CSS/display

In this case, we use the ../ writing convention — inherited from the UNIX file system world — to tell the browser we want to go up from one directory. Here we want to reach this URL: https://developer.mozilla.org/en-US/docs/Learn/../CSS/display, which can be simplified to: https://developer.mozilla.org/en-US/docs/CSS/display

Semantic URLs

Despite their very technical flavor, URLs represent a human-readable entry point for a Web site. They can be memorized, and anyone can enter them into a browser's address bar. People are at the core of the Web, and so it is considered best practice to build what is called semantic URLs.  Semantic URLs  use words with inherent meaning that can be understood by anyone, regardless of their technical know-how.

Linguistic semantics are of course irrelevant to computers. You've probably often seen URLs that look like mashups of random characters. But there are many advantages  to creating human-readable URLs:

  • It is easier for you to manipulate them.
  • It clarifies things for users in terms of where they are, what they're doing, what they're reading or interacting with on the Web.
  • Some search engines can use those semantics to improve the classification of the associated pages.

Next steps

このリビジョンのソースコード

<p><span class="seoSummary">This article discusses Uniform Resource Locators (URLs), explaining what they are and how they're structured.</span></p>
<table class="standard-table learn-box">
 <tbody>
  <tr>
   <th scope="row">Prerequisites:</th>
   <td>You need to first know <a href="/en-US/docs/Learn/Skills/Infrastructure/How_the_Internet_works">how the Internet works</a>, <a href="/en-US/docs/Learn/Skills/Infrastructure/What_is_a_Web_server">what a Web server is</a> and <a href="/en-US/docs/Learn/skills/Infrastructure/Understanding_links_on_the_web">the concepts behind links on the web</a>.</td>
  </tr>
  <tr>
   <th scope="row">Objective:</th>
   <td>You will learn what a URL is and how it works on the Web.</td>
  </tr>
 </tbody>
</table>
<h2 id="Summary">Summary</h2>
<p><span class="seoSummary">With {{Glossary("Hypertext")}} and {{Glossary("HTTP")}}, <strong><dfn>URL</dfn></strong> is one of the key concepts of the Web. It is the mechanism used by {{Glossary("Browser","browsers")}} to retrieve any published resource on the web.</span></p>
<p><strong>URL</strong> stands for <em>Uniform Resource Locator</em>. A URL is nothing more than the address of a given unique resource on the Web. In theory, each valid URL points to a unique resource. Such resources can be an HTML page, a CSS document, an image, etc. In practice, there are some exceptions, the most common being a URL pointing to a resource that no longer exists or that has moved. As the resource represented by the URL and the URL itself are handled by the Web server, it is up to the owner of the web server to carefully manage that resource and its associated URL.</p>
<h2 id="Active_Learning">Active Learning</h2>
<p>TBD</p>
<h2 id="Deeper_dive">Deeper dive</h2>
<h3 id="Basics.3A_anatomy_of_a_URL">Basics: anatomy of a URL</h3>
<p>Here are some examples of URLs:</p>
<pre>
https://developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Learn/
https://developer.mozilla.org/en-US/search?q=URL</pre>
<p>Any of those URLs can be typed into your browser's address bar to tell it to load the associated page (resource).</p>
<p>A URL is composed of different parts, some mandatory and others optional. Let's see the most important parts using the following URL:</p>
<pre>
http://www.example.com:80/path/to/myfile.html?key1=value1&amp;key2=value2#SomewhereInTheDocument</pre>
<dl>
 <dt>
  <img alt="Protocol" src="https://mdn.mozillademos.org/files/8013/mdn-url-protocol@x2.png" style="width: 440px; height: 70px;" /></dt>
 <dd>
  <code>http://</code> is the protocol. It indicates which protocol the browser must use. Usually it is the HTTP protocol or its secured version, HTTPS. The Web requires one of these two, but browsers also know how to handle other protocols such as <code>mailto:</code> (to open a mail client) or <code>ftp:</code> to handle file transfer, so don't be surprised if you see such protocols.</dd>
 <dt>
  <img alt="Domaine Name" src="https://mdn.mozillademos.org/files/8015/mdn-url-domain@x2.png" style="width: 440px; height: 70px;" /></dt>
 <dd>
  <code>www.example.com</code> is the domain name. It indicates which Web server is being requested. Alternatively, it is possible to directly use an {{Glossary("IP address")}}, but because it is less convenient, it is not often used on the Web.</dd>
 <dt>
  <img alt="Port" src="https://mdn.mozillademos.org/files/8017/mdn-url-port@x2.png" style="width: 440px; height: 70px;" /></dt>
 <dd>
  <code>:80</code> is the port. It indicates the technical "gate" used to access the resources on the web server. It is usually omitted if the web server use the standard ports of the HTTP protocol (80 for HTTP and 443 for HTTPS) to grant access to its resources. Otherwise it is mandatory.</dd>
 <dt>
  <img alt="Path to the file" src="https://mdn.mozillademos.org/files/8019/mdn-url-path@x2.png" style="width: 440px; height: 70px;" /></dt>
 <dd>
  <code>/path/to/myfile.html</code> is the path to the resource on the Web server. In the early days of the Web, a path like this represented a physical file location on the Web server. Nowadays, it is mostly an abstraction handled by Web servers without any physical reality.</dd>
 <dt>
  <img alt="Parameters" src="https://mdn.mozillademos.org/files/8021/mdn-url-parameters@x2.png" style="width: 440px; height: 70px;" /></dt>
 <dd>
  <code>?key1=value1&amp;key2=value2</code> are extra parameters provided to the Web server. Those parameters are a list of key/value pairs separated with the <code>&amp;</code> symbol. The Web server can use those parameters to do extra stuff before returning the resource. Each Web server has its own rules regarding parameters, and the only reliable way to know if a specific Web server is handling parameters is by asking the Web server owner.</dd>
 <dt>
  <img alt="Anchor" src="https://mdn.mozillademos.org/files/8023/mdn-url-anchor@x2.png" style="width: 440px; height: 70px;" /></dt>
 <dd>
  <code>#SomewhereInTheDocument</code> is an anchor to another part of the resource itself. An anchor represents a sort of "bookmark" inside the resource, giving the browser the directions to show the content located at that "bookmarked" spot. On an HTML document, for example, the browser will scroll to the point where the anchor is defined; on a video or audio document, the browser will try to go to the time the anchor represents.</dd>
</dl>
<p>{{Note('There are <a href="http://en.wikipedia.org/wiki/Uniform_Resource_Locator">some extra parts and some extra rules</a> regarding URLs, but they are not relevant for regular users or Web developers. Don\'t worry about this, you don\'t need to know them to build and use fully functional URLs.')}}</p>
<p>You might think of a URL like a regular postal mail address: the <em>protocol</em> represents the postal service you want to use, the <em>domain name</em> is the city or town,&nbsp; and the <em>port</em> is like the zip code; the <em>path</em> represents the building where your mail should be delivered; the <em>parameters</em> represent extra information such as the number of the apartment in the building; and, finally, the <em>anchor</em> represents the actual person to whom you've addressed your mail.</p>
<h3 id="How_to_use_URLs">How to use URLs</h3>
<p>Any URL can be typed right inside the browser's address bar to get to the resource behind it. But this is only the tip of the iceberg!</p>
<p>The {{Glossary("HTML")}} language — <a href="/en-US/docs/Learn/HTML/HTML_tags">which will be discussed later on</a> — makes extensive use of URLs:</p>
<ul>
 <li>to create links to other documents with the {{HTMLElement("a")}} tag;</li>
 <li>to link a document with its related resources through various tags such as {{HTMLElement("link")}} or {{HTMLElement("script")}};</li>
 <li>to display medias such as images (with the {{HTMLElement("img")}} tag), videos (with the {{HTMLElement("video")}} tag), sounds and music (with the {{HTMLElement("audio")}} tag), etc.;</li>
 <li>to display other HTML documents with the {{HTMLElement("iframe")}} tag.</li>
</ul>
<p>Other technologies, such as {{Glossary("CSS")}} or {{Glossary("JavaScript")}}, use URLs extensively, and these are really the heart of the Web.</p>
<h3 id="Absolute_URLs_vs_relative_URLs">Absolute URLs vs relative URLs</h3>
<p>What we saw above is called an <em>absolute URL, </em>but there is also something called a <em>relative URL</em>. Let's examine what that distinction means in more detail.</p>
<p>The required parts of a URL depend to a great extent on the context in which the URL is used. In your browser's address bar, a URL doesn't have any context, so you must provide a full (or <em>absolute</em>) URL, like the ones we saw above. You don't need to include the protocol (the browser uses HTTP by default) or the port (which is only required when the targeted Web server is using some unusual port), but all the other parts of the URL are necessary.</p>
<p>When a URL is used within a document, such as in an HTML page,&nbsp; things are a bit different. Because the browser already has the document's own URL, it can use this information to fill in the missing parts of any URL available inside that document. We can differentiate between an <em>absolute URL</em> and a <em>relative URL</em> by looking only at the <em>path</em> part of the URL. If the path part of the URL starts with the <code>"/</code>" character, the browser will fetch that resource from the top root of the server, without reference to the context given by the current document.</p>
<p>Let's look at some examples to make this clearer.</p>
<h4 id="Examples_of_absolute_URLs">Examples of absolute URLs</h4>
<dl>
 <dt>
  Full URL (the same as the one we used before)</dt>
 <dd>
  <pre>
https://developer.mozilla.org/en-US/docs/Learn</pre>
 </dd>
 <dt>
  Implicit protocol</dt>
 <dd>
  <pre>
//developer.mozilla.org/en-US/docs/Learn</pre>
  <p>In this case, the browser will call that URL with the same protocol as the the one used to load the document hosting that URL.</p>
 </dd>
 <dt>
  Implicit domain name</dt>
 <dd>
  <pre>
/en-US/docs/Learn</pre>
  <p>This is the most common use case for an absolute URL within an HTML document. The browser will use the same protocol and the same domain name as the one used to load the document hosting that URL. <strong>Note:</strong> <em>it isn't possible to omit the domain name without omitting the protocol as well</em>.</p>
 </dd>
</dl>
<h4 id="Examples_of_relative_URLs">Examples of relative URLs</h4>
<p>To better understand the following examples, let's assume that the URLs are called from within the document located at the following URL: <code>https://developer.mozilla.org/en-US/docs/Learn</code></p>
<dl>
 <dt>
  Sub-resources</dt>
 <dd>
  <pre>
Skills/Infrastructure/Understanding_URLs
</pre>
  Because that URL does not start with <code>/</code>, the browser will attempt to find the document in a sub-directory of the one containing the current resource. So in this example,&nbsp; we really want to reach this URL: <code>https://developer.mozilla.org/en-US/docs/Learn/Skills/Infrastructure/Understanding_URLs</code></dd>
 <dt>
  Going back in the directory tree</dt>
 <dd>
  <pre>
../CSS/display</pre>
  <p>In this case, we use the <code>../</code> writing convention — inherited from the UNIX file system world — to tell the browser we want to go up from one directory. Here we want to reach this URL: <code>https://developer.mozilla.org/en-US/docs/Learn/../CSS/display</code>, which can be simplified to: <code>https://developer.mozilla.org/en-US/docs/CSS/display</code></p>
 </dd>
</dl>
<h3 id="Semantic_URLs">Semantic URLs</h3>
<p>Despite their very technical flavor, URLs represent a human-readable entry point for a Web site. They can be memorized, and anyone can enter them into a browser's address bar. People are at the core of the Web, and so it is considered best practice to build what is called <a href="http://en.wikipedia.org/wiki/Semantic_URL"><em>semantic URLs</em></a>.&nbsp; Semantic URLs&nbsp; use words with inherent meaning that can be understood by anyone, regardless of their technical know-how.</p>
<p>Linguistic semantics are of course irrelevant to computers. You've probably often seen URLs that look like mashups of random characters. But there are many advantages&nbsp; to creating human-readable URLs:</p>
<ul>
 <li>It is easier for you to manipulate them.</li>
 <li>It clarifies things for users in terms of where they are, what they're doing, what they're reading or interacting with on the Web.</li>
 <li>Some search engines can use those semantics to improve the classification of the associated pages.</li>
</ul>
<h2 id="Next_steps">Next steps</h2>
<ul>
 <li><a href="/en-US/docs/Learn/Skills/Infrastructure/Understanding_domain_names">Understanding domain names</a></li>
</ul>
このリビジョンへ戻す