App for Cloudflare® Pro

App for Cloudflare® Pro 1.8.3

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).
 
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).
When does the plugin modify the URL of the posts to point to the newR2 url? Is it done automatically or do I have to run some command?
 
It will change the URL of the media object in real-time (as they are migrated in the case of existing images being migrated).

Be aware that only trickles down to posts that used the normal WordPress media blocks. If media blocks were bypassed (like a post done with raw HTML with <img /> tags, those wouldn’t be changed (it doesn’t go in and try to find non-standard places/ways they may have been used). For those, the redirect rules is the best way to handle (that will work even for external sites that may have used/direct linked to the media).

The Redirect Rule option isn’t automatic, you would add it to your Cloudflare account (not everyone would necessary want it).
 
It will change the URL of the media object in real-time (as they are migrated in the case of existing images being migrated).

Be aware that only trickles down to posts that used the normal WordPress media blocks. If media blocks were bypassed (like a post done with raw HTML with <img /> tags, those wouldn’t be changed (it doesn’t go in and try to find non-standard places/ways they may have been used). For those, the redirect rules is the best way to handle (that will work even for external sites that may have used/direct linked to the media).

The Redirect Rule option isn’t automatic, you would add it to your Cloudflare account (not everyone would necessary want it).
I'm migrating with rclone so how should I proceed in that case?
 
I'm migrating with rclone so how should I proceed in that case?
Migrating with rclone is going to be a little tricky because it’s more than just the underlying files (there are changes to the meta data record in the WordPress database for each media item that’s it’s R2). Certainly not an impossible task if rclone is the only option, but it would be a little work.

The plugin has a WP-CLI option built in to be able to migrate media in bulk with multiple processes (faster) if you have CLI access to your server.

But to answer the question about redirects, it wouldn’t be any different if you go the rclone route (same settings shown in earlier part of this thread).
 
Migrating with rclone is going to be a little tricky because it’s more than just the underlying files (there are changes to the meta data record in the WordPress database for each media item that’s it’s R2). Certainly not an impossible task if rclone is the only option, but it would be a little work.
I was hoping to use rclone and then use the migration tool to update the records in the database. Do you think that could work? Rclone seems much faster than WP-CLI
 
I was hoping to use rclone and then use the migration tool to update the records in the database. Do you think that could work? Rclone seems much faster than WP-CLI
Honestly I haven’t tried it, but it would *probably* work (try it with a backup).

The WP-CLI command has a specific option for the number of processes to use, so you can bump that up if your server can support it (it defaults to just a single process).
 
Honestly I haven’t tried it, but it would *probably* work (try it with a backup).

The WP-CLI command has a specific option for the number of processes to use, so you can bump that up if your server can support it (it defaults to just a single process).
So what does this modification do then?
 
Yep, as long as you do the the modifications it would be good.
So the process would be to upload to R2 using rclone
Then comment this line:
Code:
//$cloudflareRepo->addR2Object($bucketName, $sitePrefix . $filename, $contents);
and finally migrate to R2 using the popup:
1712342555533.png


Is that everything? I'd like you to confirm all the steps because it's quite confusing
 
Last edited:
So the process would be to upload to R2 using rclone
Then comment this line:
Code:
//$cloudflareRepo->addR2Object($bucketName, $sitePrefix . $filename, $contents);
and finally migrate to R2 using the popup:
View attachment 26


Is that everything? I'd like you to confirm all the steps because it's quite confusing
Ya that should be it. It’s definitely a non-standard way of doing it (I wouldn’t recommend doing it that way normally, best to use WP-CLI for most cases even if it’s not quite as fast).

But yes.. that should work.
 
Back
Top