この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:5
ページ更新者:T
更新日時:2026-06-11 07:10:02

タイトル: aタグのhrefの記載方法
SEOタイトル: HTML a タグ href 完全ガイド — 絶対/相対/フラグメント/Laravel route

この本文には実行可能なタグ等が含まれるため、安全のためHTMLソースとして表示しています。本文データは変更していません。

<table border="1" cellpadding="6" cellspacing="0" style="border-collapse:collapse;background:#fffbea;border:1px solid #f4c430;margin:1em 0;width:100%"><tr><td style="background:#f4c430;color:#000;font-weight:bold;padding:8px">この記事の要点</td></tr><tr><td style="padding:10px"><ul><li><strong>絶対 URL</strong>: <code>https://example.com/page</code> ← 外部サイト</li><li><strong>ルート相対</strong>: <code>/path/page</code> ← 同一ドメイン、サイトルートから</li><li><strong>相対 URL</strong>: <code>../page</code> ← 現在のページからの相対</li><li><strong>フラグメント</strong>: <code>#section</code> ← ページ内ジャンプ</li><li><strong>特殊スキーム</strong>: <code>mailto:</code> / <code>tel:</code> / <code>sms:</code></li></ul></td></tr></table>
<h2>href の基本</h2>

<p>HTML <code>&lt;a&gt;</code> タグの <strong>href 属性</strong>はリンク先 URL を指定します。URL の書き方は複数あり、用途で使い分けます。</p>

<h2>主な href の種類</h2>

<table border="1" cellpadding="6" cellspacing="0" style="border-collapse:collapse;width:100%">
<thead><tr style="background:#e3f2fd"><th>種類</th><th>例</th><th>解決先</th></tr></thead>
<tbody>
<tr><td>絶対 URL</td><td><code>https://example.com/page</code></td><td>そのまま</td></tr>
<tr><td>プロトコル相対</td><td><code>//example.com/page</code></td><td>現在のスキーム (http/https)</td></tr>
<tr><td>ルート相対</td><td><code>/path/page</code></td><td>サイトルートから</td></tr>
<tr><td>相対 (同階層)</td><td><code>page.html</code></td><td>現在ディレクトリから</td></tr>
<tr><td>相対 (親階層)</td><td><code>../page.html</code></td><td>1 階層上から</td></tr>
<tr><td>フラグメント</td><td><code>#section</code></td><td>同ページ内</td></tr>
<tr><td>クエリ</td><td><code>?id=1</code></td><td>現在 URL にクエリ追加</td></tr>
<tr><td>mailto:</td><td><code>mailto:foo@example.com</code></td><td>メールクライアント起動</td></tr>
<tr><td>tel:</td><td><code>tel:+81-3-1234-5678</code></td><td>電話アプリ起動</td></tr>
<tr><td>javascript:</td><td><code>javascript:void(0)</code></td><td>JS 実行 (非推奨)</td></tr>
</tbody>
</table>

<h2>絶対 URL</h2>

<p>外部サイトへのリンク、SNS シェア、メールに貼るリンク等は絶対 URL:</p>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-html">&lt;a href=&quot;https://example.com/article/123&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;
  外部記事
&lt;/a&gt;

&lt;!-- プロトコル相対 (https/http どちらでも追従) --&gt;
&lt;!-- ※ 現代はすべて https なので非推奨 --&gt;
&lt;a href=&quot;//cdn.example.com/lib.js&quot;&gt;CDN&lt;/a&gt;</code></pre>
<h2>ルート相対 (推奨)</h2>

<p>同一ドメイン内のリンクには<strong>ルート相対</strong>が無難:</p>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-html">&lt;!-- 現在の URL が何であれ /about に飛ぶ --&gt;
&lt;a href=&quot;/about&quot;&gt;About&lt;/a&gt;
&lt;a href=&quot;/products/laptop&quot;&gt;ノートパソコン&lt;/a&gt;

&lt;!-- スラッシュで始めるのがポイント --&gt;
&lt;a href=&quot;/css/main.css&quot;&gt;スタイル&lt;/a&gt;</code></pre>
<p>利点:</p>
<ul>
<li>現在のページ階層に依存しないので壊れにくい</li>
<li>ステージング / 本番のドメインが違っても同じパスで動く</li>
<li>SSR / 静的サイトジェネレータと相性◎</li>
</ul>

<h2>相対 URL</h2>

<p>現在のページからの相対パス。ファイルシステムベースのサイトで使います:</p>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-html">&lt;!-- 現在 /blog/2024/article.html の場合 --&gt;
&lt;a href=&quot;next.html&quot;&gt;/blog/2024/next.html へ&lt;/a&gt;
&lt;a href=&quot;../2023/old.html&quot;&gt;/blog/2023/old.html へ&lt;/a&gt;
&lt;a href=&quot;../../about.html&quot;&gt;/about.html へ&lt;/a&gt;
&lt;a href=&quot;./page.html&quot;&gt;同階層 (= page.html)&lt;/a&gt;</code></pre>
<p>注意: ベース URL は <code>&lt;base href&gt;</code> タグで変更可能 (後述)。</p>

<h2>フラグメント (ページ内リンク)</h2>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-html">&lt;!-- 同ページの id=&quot;section1&quot; にジャンプ --&gt;
&lt;a href=&quot;#section1&quot;&gt;セクション 1 へ&lt;/a&gt;

&lt;h2 id=&quot;section1&quot;&gt;セクション 1&lt;/h2&gt;

&lt;!-- 別ページの特定セクションへ --&gt;
&lt;a href=&quot;/article/123#comments&quot;&gt;コメント欄&lt;/a&gt;

&lt;!-- 最上部へ --&gt;
&lt;a href=&quot;#&quot;&gt;先頭へ&lt;/a&gt;
&lt;a href=&quot;#top&quot;&gt;先頭へ (top はブラウザ既定)&lt;/a&gt;

&lt;!-- スクロール挙動を JS で滑らかに --&gt;
&lt;style&gt;
  html { scroll-behavior: smooth; }
&lt;/style&gt;</code></pre>
<h2>クエリパラメータ</h2>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-html">&lt;!-- 現在 URL にクエリだけ変更 --&gt;
&lt;a href=&quot;?page=2&quot;&gt;次ページ&lt;/a&gt;

&lt;!-- 既存のクエリも維持したい場合は JS 必須 --&gt;
&lt;a href=&quot;?page=2&amp;sort=date&quot;&gt;並び替え&lt;/a&gt;

&lt;!-- 検索フォームのリンク --&gt;
&lt;a href=&quot;/search?q=html&amp;lang=ja&quot;&gt;HTML 検索&lt;/a&gt;</code></pre>
<h2>特殊スキーム</h2>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-html">&lt;!-- メール --&gt;
&lt;a href=&quot;mailto:contact@example.com&quot;&gt;お問い合わせ&lt;/a&gt;
&lt;a href=&quot;mailto:contact@example.com?subject=資料請求&amp;body=お世話になります&quot;&gt;資料請求&lt;/a&gt;
&lt;a href=&quot;mailto:a@x.com,b@x.com?cc=c@x.com&amp;bcc=d@x.com&quot;&gt;複数宛先&lt;/a&gt;

&lt;!-- 電話 (モバイルでタップで発信) --&gt;
&lt;a href=&quot;tel:+81-3-1234-5678&quot;&gt;03-1234-5678&lt;/a&gt;
&lt;a href=&quot;tel:0312345678&quot;&gt;国内表記でも可&lt;/a&gt;

&lt;!-- SMS --&gt;
&lt;a href=&quot;sms:+81-90-1234-5678?body=こんにちは&quot;&gt;SMS&lt;/a&gt;

&lt;!-- ファイルダウンロード --&gt;
&lt;a href=&quot;/files/manual.pdf&quot; download&gt;マニュアルをダウンロード&lt;/a&gt;
&lt;a href=&quot;/files/data.csv&quot; download=&quot;export-2024.csv&quot;&gt;CSV を保存&lt;/a&gt;</code></pre>
<h2>JavaScript URL (非推奨)</h2>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-html">&lt;!-- ❌ 非推奨: CSP / セキュリティで弾かれる --&gt;
&lt;a href=&quot;javascript:alert(&apos;hi&apos;)&quot;&gt;クリック&lt;/a&gt;
&lt;a href=&quot;javascript:void(0)&quot; onclick=&quot;doSomething()&quot;&gt;処理&lt;/a&gt;

&lt;!-- ✅ 推奨: button を使う --&gt;
&lt;button type=&quot;button&quot; onclick=&quot;doSomething()&quot;&gt;処理&lt;/button&gt;

&lt;!-- ✅ どうしても a タグなら --&gt;
&lt;a href=&quot;#&quot; onclick=&quot;event.preventDefault(); doSomething();&quot;&gt;処理&lt;/a&gt;</code></pre>
<h2>target / rel / download 属性</h2>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-html">&lt;!-- 新タブ (必ず rel=&quot;noopener noreferrer&quot; を付ける) --&gt;
&lt;a href=&quot;https://example.com&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;
  外部
&lt;/a&gt;

&lt;!-- iframe 内のリンクを親フレームに --&gt;
&lt;a href=&quot;/page&quot; target=&quot;_parent&quot;&gt;親に開く&lt;/a&gt;
&lt;a href=&quot;/page&quot; target=&quot;_top&quot;&gt;最上位に開く&lt;/a&gt;

&lt;!-- ダウンロード強制 --&gt;
&lt;a href=&quot;/files/report.pdf&quot; download&gt;レポート&lt;/a&gt;

&lt;!-- リンク先言語 --&gt;
&lt;a href=&quot;/en/article&quot; hreflang=&quot;en&quot;&gt;English&lt;/a&gt;

&lt;!-- リンク種類 --&gt;
&lt;a href=&quot;https://shop.example.com&quot; rel=&quot;sponsored noopener&quot;&gt;広告&lt;/a&gt;</code></pre>
<h2>Laravel での書き方</h2>

<p>Laravel では URL ヘルパー / ルートヘルパーを使うと、ルート定義変更時にも壊れません:</p>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-php">// Blade テンプレ

// 1. ルート名で生成 (推奨)
&lt;a href=&quot;{{ route(&apos;home&apos;) }}&quot;&gt;ホーム&lt;/a&gt;
&lt;a href=&quot;{{ route(&apos;article.show&apos;, [&apos;id&apos; =&gt; 123]) }}&quot;&gt;記事 123&lt;/a&gt;

// 2. URL ヘルパー
&lt;a href=&quot;{{ url(&apos;/about&apos;) }}&quot;&gt;About&lt;/a&gt;
&lt;a href=&quot;{{ url(&apos;/article/&apos; . $id) }}&quot;&gt;記事&lt;/a&gt;

// 3. asset ヘルパー (CSS / JS / 画像)
&lt;link rel=&quot;stylesheet&quot; href=&quot;{{ asset(&apos;css/app.css&apos;) }}&quot;&gt;
&lt;img src=&quot;{{ asset(&apos;images/logo.png&apos;) }}&quot; alt=&quot;ロゴ&quot;&gt;

// 4. 現在のクエリを保持
&lt;a href=&quot;{{ url()-&gt;current() }}?page=2&quot;&gt;次ページ&lt;/a&gt;
&lt;a href=&quot;{{ request()-&gt;fullUrlWithQuery([&apos;page&apos; =&gt; 2]) }}&quot;&gt;次ページ&lt;/a&gt;

// 5. ログインユーザーのプロフィールへ
&lt;a href=&quot;{{ route(&apos;users.show&apos;, auth()-&gt;id()) }}&quot;&gt;マイページ&lt;/a&gt;</code></pre>
<p>routes/web.php 側:</p>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-php">Route::get(&apos;/article/{id}&apos;, [ArticleController::class, &apos;show&apos;])-&gt;name(&apos;article.show&apos;);
Route::get(&apos;/&apos;, [HomeController::class, &apos;index&apos;])-&gt;name(&apos;home&apos;);</code></pre>
<h2><base href> (相対パスのベースを変更)</h2>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;!-- すべての相対 URL がこのベースから解決される --&gt;
  &lt;base href=&quot;https://cdn.example.com/v2/&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;!-- → https://cdn.example.com/v2/img.png --&gt;
  &lt;img src=&quot;img.png&quot;&gt;
  &lt;!-- → https://cdn.example.com/v2/page.html --&gt;
  &lt;a href=&quot;page.html&quot;&gt;ページ&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>
<p>SPA や iframe 多用ページで便利。ただし副作用 (フラグメント <code>#anchor</code> も絶対 URL 化される) があるので慎重に。</p>

<h2>href の選び方フローチャート</h2>

<ul>
<li>外部サイトへのリンク? → <strong>絶対 URL</strong> + <code>target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;</code></li>
<li>同サイト内?</li>
<ul>
<li>同ページ内ジャンプ → <strong>フラグメント (#id)</strong></li>
<li>サーバサイドルーティング (Laravel/Rails) → <strong>ルート相対</strong> (route ヘルパー推奨)</li>
<li>静的サイト (HTML 直書き) → <strong>ルート相対</strong></li>
</ul>
<li>メール / 電話アプリ起動 → <strong>mailto: / tel:</strong></li>
<li>ファイルダウンロード → <strong>絶対 or ルート相対</strong> + <code>download</code></li>
<li>JS 実行のみ → <strong>button タグ</strong>に置き換える</li>
</ul>

<h2>FAQ</h2>

<p><strong>Q: 相対 URL とルート相対 URL、どちらを使うべき?</strong><br />
A: 個別ファイルでサイト構成が変わらないなら相対でも OK。テンプレ展開やリファクタリングが多いプロジェクトは<strong>ルート相対 (または route ヘルパー)</strong> が壊れにくい。</p>

<p><strong>Q: href="" (空) はどうなる?</strong><br />
A: 現在のページを再読み込みします (POST 後の意図しない再送信に注意)。意図せず空になっていないかチェック。</p>

<p><strong>Q: SPA で a タグだと全リロードしてしまう</strong><br />
A: フレームワーク標準の <code>Link</code> コンポーネント (Next.js <code>Link</code>, Vue Router <code>RouterLink</code>) を使うと SPA 遷移になります。</p>