Skip to content

Instantly share code, notes, and snippets.

@tuliren
Last active July 20, 2024 19:46
Show Gist options
  • Save tuliren/6f0d1604fd9342fae542a8cb0a60daea to your computer and use it in GitHub Desktop.
Save tuliren/6f0d1604fd9342fae542a8cb0a60daea to your computer and use it in GitHub Desktop.
Cloudflare redirection

Cloudflare redirection

Example: redirect from old.com to new.com, and www.old.com to www.new.com

Add redirect rule

  • Go to Rules / Redirect Rules page
  • Create a new rule
  • Select Custom filter expression
  • Add matching rules
    • Field Hostname, operator equals, value old.com, or
    • Field Hostname, operator equals, value www.old.com
  • Add URL redirect rule
    • Type Dynamic, expression concat("https://www.new.com", http.request.uri.path), status code 301
  • Check Preserve query string
  • Click Deploy

Set up DNS

  • Type A, name @, content 192.0.0.1, enable Proxy
  • Type A, name www, content 192.0.0.1, enable Proxy

The key is to enable Cloudflare proxy. The actual address for the content field does not matter. Can also set up CNAME records with arbitrary content values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment