Skip to main content

Redirect

还没翻译译自英文(developers.cloudflare.com)本页 16 段都还没译

If you want to get started quickly, click on the button below.

Deploy to Cloudflare

This creates a repository in your GitHub account and deploys the application to Cloudflare Workers.

Redirect all requests to one URL

JavaScriptTypeScriptPythonRustHono

export default {
  async fetch(request) {
    const destinationURL = "https://example.com";
    const statusCode = 301;
    return Response.redirect(destinationURL, statusCode);
  },
};

Redirect requests from one domain to another

JavaScriptTypeScriptPythonRustHono

export default {
	async fetch(request) {
		const base = "https://example.com";
		const statusCode = 301;

		const url = new URL(request.url);
		const { pathname, search } = url;

		const destinationURL = `${base}${pathname}${search}`;
		console.log(destinationURL);

		return Response.redirect(destinationURL, statusCode);
	},
};