末尾のスラッシュとリダイレクトの動作
正規URLと自動リダイレクトの仕組みを理解します
「末尾のスラッシュとリダイレクトの動作」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
One Slash Changes Everything
To Flask, /about and /about/ can be two different things. That tiny trailing slash decides how a route matches and whether a redirect happens.
Route Defined With a Slash
When your route ends in a slash, it behaves like a folder. Visiting /docs/ works, and /docs gets redirected to the slashed version. 📁
@app.route('/docs/')
def docs():
return 'Docs'Route Defined Without a Slash
A route with no trailing slash behaves like a file. Here /docs works, but /docs/ returns a 404 instead of redirecting.
@app.route('/docs')
def docs():
return 'Docs'The Folder-Style Redirect
For folder-style routes, Flask helpfully redirects the slashless URL to the canonical one. This keeps a single canonical address for that page.
No Redirect the Other Way
The kindness only flows one direction. A file-style route will not redirect from /docs/ to /docs; the extra slash simply yields a 404.
Why Canonical URLs Matter
One canonical URL per page avoids duplicate addresses. That helps caching, analytics, and SEO, since search engines see a single canonical link.
url_for Picks the Right Form
Let url_for generate links and you always get the canonical slash form. You never have to remember which routes end in a slash.
url_for('docs') # uses the route's exact formPick a Convention and Hold It
Choose one style across your app, often no trailing slash for endpoints. A steady convention means fewer surprise 404s for your users.
Redirects Are a Client Round Trip
A redirect sends a 301 or 308 back, and the browser then requests the new URL. That is one extra round trip you avoid by linking correctly.
POST and the Slash Trap
Slash redirects on a POST can drop the body or change the method. Always post to the exact canonical URL to keep your data intact.
Test Both Forms
When debugging a missing page, try the URL with and without the slash. The difference often reveals a mismatch between your link and your route.
Quick Check
Your route is @app.route('/docs') with no trailing slash. What does visiting /docs/ return?
Recap: Slashes Are Decisions
A trailing slash makes a route folder-like and redirects to its canonical form; without one it is strict. Lean on url_for and stay consistent. 🎉
よくある質問
「末尾のスラッシュとリダイレクトの動作」レッスンは無料ですか?
はい。「末尾のスラッシュとリダイレクトの動作」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「末尾のスラッシュとリダイレクトの動作」で何を学びますか?
正規URLと自動リダイレクトの仕組みを理解します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「末尾のスラッシュとリダイレクトの動作」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- パスから変数を取得する
- 型コンバーター:int、float、string、path
- url_forでURLを構築する
- 末尾のスラッシュとリダイレクトの動作