Define URL. Explain types of URL

SOLUTION

URL — Detailed Explanation

A URL (Uniform Resource Locator) is the address used to locate and retrieve a resource on the internet. It tells a client (browser, API client, etc.) what protocol to use, which host to contact, where the resource lives, and optionally how to pass extra data or jump to a fragment inside the resource.

Components of a URL

Example URL:

Breakdown:

  • Scheme (protocol): https — how to access the resource (HTTP over TLS here).

  • User info (optional): user:pass — username/password (discouraged in practice).

  • Host (domain or IP): example.com — identifies the server.

  • Port (optional): :8080 — TCP port; default ports are implied (HTTP 80, HTTPS 443).

  • Path: /dir/sub/page.html — location of the resource on the server.

  • Query string: ?q=search+term&lang=en — key/value data sent to the server (commonly for GET requests).

  • Fragment: #section2 — a client-side anchor; not sent to server (used to jump to a part of the resource).

Types of URLs (with examples)

1. Absolute URL

A complete URL including scheme and host.

  • Example: https://www.example.com/about/index.html

  • Use when linking to resources on other sites or when you need a full address.

2. Relative URLs

Paths relative to the current document or site. They do not include scheme/host.

  • Document-relative (path-relative): images/pic.jpg

    • If current page is https://site.com/dir/page.html, this resolves to https://site.com/dir/images/pic.jpg.

  • Root-relative: /images/pic.jpg

    • Resolves to https://site.com/images/pic.jpg regardless of current page path.

  • Parent-relative: ../assets/style.css

    • Moves up a directory before resolving.

3. Protocol-relative URL

Starts with // and inherits the current page’s protocol.

  • Example: //cdn.example.com/lib.js

  • If the page is https, this becomes https://cdn.example.com/lib.js.

4. Data URL

Embeds data directly in the URL (useful for small inline images).

  • Example: data:text/plain;base64,SGVsbG8sIHdvcmxkIQ==

5. File URL

References local filesystem resources (used locally or in some apps).

  • Example: file:///C:/Users/name/document.txt or file:///home/user/doc.txt

6. Mailto, Tel, FTP and other scheme URLs

  • mailto: for email links: mailto:someone@example.com

  • tel: for phone links on mobile: tel:+911234567890

  • ftp: FTP resources: ftp://ftp.example.com/pub/file.zip
    These use schemes other than HTTP/HTTPS and are handled accordingly.

How Relative URLs are Resolved

Browsers compute the full URL using the base URL (the current page’s URL or the <base> tag if present).

Examples (base https://site.com/dir/page.html):

  • images/pic.jpghttps://site.com/dir/images/pic.jpg

  • ../img/logo.pnghttps://site.com/img/logo.png

  • /css/style.csshttps://site.com/css/style.css

Query Strings and Fragments

  • Query string (?key=value&k2=v2) passes parameters to the server; commonly used in GET requests and RESTful filtering.

  • Fragment (#anchor) is handled client-side (scrolling to an element, single-page app routing) and is not sent to the server.

URL Encoding (Percent-encoding)

Characters not allowed in URLs (spaces, <, >, non-ASCII, etc.) must be encoded:

  • Space → %20 (or + in some query contexts)

  • Example: https://site.com/search?q=hello%20world

Use encoding to avoid ambiguity and to safely include special characters.

Security & Best Practices

  • Prefer HTTPS to protect data in transit.

  • Do not include sensitive data (passwords, tokens, credit-card info) in URLs — query strings are logged and cached.

  • Use canonical URLs to avoid duplicate content issues (important for SEO).

  • Limit URL length (some servers/browsers have practical limits).

  • Validate and sanitize URL-derived input on the server to prevent injection attacks.

URL vs URI (brief)

  • URI (Uniform Resource Identifier) is a broader term that identifies a resource; a URL is a type of URI that gives a location you can retrieve the resource from.

Leave a Reply

Your email address will not be published. Required fields are marked *

sign up!

We’ll send you the hottest deals straight to your inbox so you’re always in on the best-kept software secrets.