---
title: Redirect from one domain to another · Cloudflare Rules docs
description: Redirect all requests from one domain to another domain.
lastUpdated: 2024-08-21T16:27:56.000Z
source_url:
  html: https://3fb19f37.preview.developers.cloudflare.com/rules/snippets/examples/redirect-replaced-domain/index.md
  md: https://3fb19f37.preview.developers.cloudflare.com/rules/snippets/examples/redirect-replaced-domain/
---

```js
export default {
  async fetch(request) {
    // Define variables to use in the response redirect.
    const base = "https://example.com";
    const statusCode = 301;


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


    // Define a "pathname" and "search" variables, extracting their values from the cloned URL.
    const { pathname, search } = url;


    // Define the destination URL using the variables you declared previously.
    const destinationURL = `${base}${pathname}${search}`;
    console.log(destinationURL);


    // Respond with the redirect.
    return Response.redirect(destinationURL, statusCode);
  },
};
```
