URLとURI
URLとURI完全ガイド
Section titled “URLとURI完全ガイド”URLとURIは、Web上のリソースの場所を表す方法です。Web開発において重要な概念を詳しく解説します。
1. URLとURIとは
Section titled “1. URLとURIとは”URI(Uniform Resource Identifier)
Section titled “URI(Uniform Resource Identifier)”リソースを一意に識別するための識別子です。
URI = URL + URNURL(Uniform Resource Locator)
Section titled “URL(Uniform Resource Locator)”リソースの場所を表すURIの一種です。
https://example.com:443/path/to/resource?query=value#fragmentURN(Uniform Resource Name)
Section titled “URN(Uniform Resource Name)”リソースの名前を表すURIの一種です(あまり使われない)。
urn:isbn:978-4-123456-78-92. URLの構造
Section titled “2. URLの構造”URLの各部分
Section titled “URLの各部分”https://example.com:443/path/to/resource?query=value#fragment^^^^^ ^^^^^^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^スキーム ホスト ポート パス クエリ フラグメントスキーム(プロトコル)
Section titled “スキーム(プロトコル)”http:// - HTTPプロトコルhttps:// - HTTPSプロトコルftp:// - FTPプロトコルmailto: - メールアドレスfile:// - ローカルファイルexample.com - ドメイン名93.184.216.34 - IPアドレスlocalhost - ローカルホスト:80 - HTTP(デフォルト、省略可能):443 - HTTPS(デフォルト、省略可能):3000 - カスタムポート:8080 - 開発サーバーなど/path/to/resource/index.html/api/usersクエリパラメータ
Section titled “クエリパラメータ”?key=value&key2=value2?search=query&page=1フラグメント
Section titled “フラグメント”#section1#top3. URLエンコーディング
Section titled “3. URLエンコーディング”パーセントエンコーディング
Section titled “パーセントエンコーディング”スペース: %20日本語: %E6%97%A5%E6%9C%AC%E8%AA%9E&: %26=: %3D実務での使用例
Section titled “実務での使用例”// JavaScriptでのエンコーディングencodeURIComponent('Hello World') // 'Hello%20World'decodeURIComponent('Hello%20World') // 'Hello World'
// URLの構築const baseURL = 'https://api.example.com';const endpoint = '/users';const params = { name: 'John Doe', age: 30 };const queryString = new URLSearchParams(params).toString();const url = `${baseURL}${endpoint}?${queryString}`;// https://api.example.com/users?name=John+Doe&age=304. 相対URLと絶対URL
Section titled “4. 相対URLと絶対URL”https://example.com/path/to/resource- 完全なURL
- どこからでも同じリソースを指す
/path/to/resource - ルート相対../parent/resource - 親ディレクトリ./sibling/resource - 現在のディレクトリresource - 現在のディレクトリ(省略形)5. 実務でのURL操作
Section titled “5. 実務でのURL操作”URLの解析
Section titled “URLの解析”// JavaScriptでのURL解析const url = new URL('https://example.com:443/path?query=value#fragment');
console.log(url.protocol); // 'https:'console.log(url.host); // 'example.com:443'console.log(url.hostname); // 'example.com'console.log(url.port); // '443'console.log(url.pathname); // '/path'console.log(url.search); // '?query=value'console.log(url.hash); // '#fragment'クエリパラメータの操作
Section titled “クエリパラメータの操作”const url = new URL('https://example.com/search');url.searchParams.set('q', 'search query');url.searchParams.set('page', '1');console.log(url.toString());// https://example.com/search?q=search+query&page=1
// クエリパラメータの取得const q = url.searchParams.get('q');const page = url.searchParams.get('page');これで、URLとURIの仕組みと実務での使い方を理解できるようになりました。