App for Cloudflare® Pro

App for Cloudflare® Pro 1.8.1

Redirecting old WordPress media URLs to new URLs after migrating media to R2

digitalpoint

Developer
Staff member
For sites that migrate existing media to R2, it makes sense in most cases to redirect the old URL to the new URL. You are able to do it easily with a single Cloudflare Redirect Rule (in your Cloudflare dashboard under Rules -> Redirect Rules -> Create Rule) like so:

Rule nameRedirect local media to R2 bucket
If...Custom filter expression
When incoming requests match...Field: URI Path
Operator: starts with
Value: /wp-content/uploads/
Then...Type: Dynamic
Expression: concat("https://{YOUR_R2_BUCKET_PUBLIC_DOMAIN}", substring(http.request.uri.path, 19))
Status code: 301

Assuming your media was stored in the default WordPress location (in the /wp-content/uploads/ folder), that should handle the redirects without needing to do any config on your origin web server (all handled by the single Redirect rule in Cloudflare).
 
Back
Top