Redirect root domain to www using CloudFlare

Setup a redirect using CloudFlare to redirect requests from the root domain to the www subdomain.

Redirect root domain to www using CloudFlare

Login to your CloudFlare account and select the domain you want to apply the redirect rule to. The free-tier plan is sufficient for this setup and allow us to setup 10 rules for free.

Goto “Rules” -> “Redirect rules” and create a new rule using the following settings:

  • Rule name: Redirect root to www
  • When incoming requests match: Custom filter expression
    • Field: Hostname
    • Operator: equals
    • Value: domain.com

Expression preview:

(http.host eq "domain.com")
  • Then
    • Type: Dynamic
    • Expression: concat("https://www.domain.com", http.request.uri.path)
    • Status code: 301
    • Preserve query string: Checked

Important

For the CloudFlare redirect rules to work you have to make sure that the DNS records for the root domain is proxied through CloudFlare. This is indicated by the orange cloud icon in the DNS records.

Test

Test the redirect rule using curl:

curl -i https://domain.com/en/test

HTTP/2 301
date: Sat, 02 Mar 2024 18:40:23 GMT
location: https://www.domain.com/en/test
cache-control: max-age=3600
expires: Sat, 02 Mar 2024 19:40:23 GMT
server: cloudflare

Redirect rules documentation

More Articles

Generate Commit Messages with Ollama in Neovim

Generate Commit Messages with Ollama in Neovim

Generate conventional commit messages from staged diffs using Ollama. Run it locally for privacy and offline, or use cloud models for speed.

Adding Umami analytics to the OpenClaw morning brief

Adding Umami analytics to the OpenClaw morning brief

An agent skill that fetches traffic data from Umami, and how it fits into a daily automated briefing.

Setting up Google Calendar sync for OpenClaw

Setting up Google Calendar sync for OpenClaw

How I set up read-only Google Calendar sync for my personal AI assistant running on a home server VM.

Better Clipboard Handling in Claude Code

Better Clipboard Handling in Claude Code

A plugin that makes clipboard operations in Claude Code more reliable and natural to use.

Get notified when Claude Code needs your input

Get notified when Claude Code needs your input

Stop constantly checking your terminal. Set up notifications that alert you when Claude Code is ready for your input.

Auto-format generated Code with Claude Code Hooks

Auto-format generated Code with Claude Code Hooks

How to set up a PostToolUse hook in Claude Code to automatically run prettier after every file edit or write operation.

PWA Web Share Target on Android: The Absolute URL Fix

PWA Web Share Target on Android: The Absolute URL Fix

Getting the Web Share Target API to work on Android PWAs can be frustrating. Your manifest looks correct, but the app never appears in the share sheet. Here is what finally worked.

Standard function keys on external keyboards in MacOS

Standard function keys on external keyboards in MacOS

Configure Karabiner Elements to use standard function keys on external keyboards while keeping media keys on your MacBook.

How to Clear Cloudflare Cache using a webhook

How to Clear Cloudflare Cache using a webhook

Automatically purge Cloudflare cache using webhooks and API tokens.