コンテンツにスキップ

末尾スラッシュ

すべてのコアミドルウェアは 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/v5@5196b9b

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

パッケージのソースコードのフィールド
フィールド型説明ソース
Skipper Skipper Skipper defines a function to skip middleware. L17
RedirectCode int Status 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.MiddlewareFunc L29
func AddTrailingSlashWithConfig(config AddTrailingSlashConfig) echo.MiddlewareFunc L34

RemoveTrailingSlashConfig · github.com/labstack/echo/v5@5196b9b

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

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

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

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

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