Skip to content

非公式本サイトは非公式の日本語ドキュメントであり、Cloudflare 公式サイトではありません。最新情報はdevelopers.cloudflare.comをご確認ください。

マップオブジェクトに基づく一括リダイレクト

リクエスト URL に対応するマップオブジェクトに基づいて、特定の URL へリクエストをリダイレクトします。

最終更新 Markdown で表示Agent セットアップ
export default {
	async fetch(request) {
		// Define a variable with the hostname that needs to be redirected.
		const externalHostname = "example.com";

		// Define the map object. Replace the sources (/pathX) and targets (/redirectX) with ones that apply to your case.
		const redirectMap = new Map([
			["/path1", "https://" + externalHostname + "/redirect1"],
			["/path2", "https://" + externalHostname + "/redirect2"],
			["/path3", "https://" + externalHostname + "/redirect3"],
			["/path4", "https://cloudflare.com"],
		]);

		// Clone the original URL.
		const requestURL = new URL(request.url);

		// Check the request path against the map and redirect accordingly.
		const path = requestURL.pathname;
		const location = redirectMap.get(path);

		if (location) {
			return Response.redirect(location, 301);
		}

		// If request path not in map, return the original request.
		return fetch(request);
	},
};

役に立ちましたか?