コンテンツにスキップ

末尾スラッシュ

すべてのコアミドルウェアは middleware パッケージに含まれています:

import "github.com/labstack/echo/v5/middleware"

Add trailing slash ミドルウェアは、リクエスト URI に末尾スラッシュを追加します。

e := echo.New()
e.Pre(middleware.AddTrailingSlash())

Remove trailing slash ミドルウェアは、リクエスト URI から末尾スラッシュを削除します。

e := echo.New()
e.Pre(middleware.RemoveTrailingSlash())
e := echo.New()
e.Use(middleware.AddTrailingSlashWithConfig(middleware.AddTrailingSlashConfig{
RedirectCode: http.StatusMovedPermanently,
}))

上の例は、リクエスト URI に末尾スラッシュを追加し、301 - StatusMovedPermanently でリダイレクトします。

AddTrailingSlashConfig · github.com/labstack/echo/[email protected]

フィールドの説明は英語のソースコードのコメントから生成されています。

パッケージのソースコードのフィールド
フィールド型説明ソース
SkipperSkipperSkipper defines a function to skip middleware.L17
RedirectCodeintStatus code to be used when redirecting the request. Optional, but when provided the request is redirected using this code. Valid status codes: [300...308]L22
パッケージのソースコードの関数
型ソース
func AddTrailingSlash() echo.MiddlewareFuncL29
func AddTrailingSlashWithConfig(config AddTrailingSlashConfig) echo.MiddlewareFuncL34

RemoveTrailingSlashConfig · github.com/labstack/echo/[email protected]

フィールドの説明は英語のソースコードのコメントから生成されています。

パッケージのソースコードのフィールド
フィールド型説明ソース
SkipperSkipperSkipper defines a function to skip middleware.L82
RedirectCodeintStatus code to be used when redirecting the request. Optional, but when provided the request is redirected using this code.L86
パッケージのソースコードの関数
型ソース
func RemoveTrailingSlash() echo.MiddlewareFuncL93
func RemoveTrailingSlashWithConfig(config RemoveTrailingSlashConfig) echo.MiddlewareFuncL98

Echo のセキュリティ更新 (v5.4.0 / v4.16.0)

Section titled “Echo のセキュリティ更新 (v5.4.0 / v4.16.0)”

末尾のスラッシュを変更するミドルウェアは、リダイレクト時にパス内の制御文字をパーセントエンコードしてから Location ヘッダーを作成します。