Implement 301 subdomain redirect with Cloudflare

RewriteCond %{HTTP_HOST} ^yourdomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com$
RewriteRule ^wp\-content\/uploads\/?(.*)$ "http\:\/\/subdomain\.yourdomain\.com\/$1" [R=301,L]

Any way I can implement this on Cloudflare?

Thanks!

Page rules

2 Likes

Can I do this only for images? I want to host my images on media.site.com, but not redirect everyone there.

Thanks!

Page rule

Match:

*yourdomain/wp-content/uploads/*

Redirect to:

https://subdomain.yourdomain/$2
1 Like

Yes, you can, you simply need to place the right pattern as your URL match.

Thank you!

1 Like

Actually, since the source is making use of two wildcards *, the target should end with $2 instead of $1.

2 Likes

Thanks. Fixed.

2 Likes

Would you recommend doing this server side or via Cloudflare?

https://www.webnots.com/move-wordpress-images-folder-to-subdomain/

I am using that guide and it’s only temporary to prevent loss of search engine image traffic.

Thank you!

If you plan to use exclusively Cloudflare it is probably performance-wise a tad better to do it on Cloudflare straight away.

1 Like

Do it at both server side and Cloudflare so if you are using CF, request doesn’t reach your server and if you some day remove CF, your website will not break.

1 Like

Okay, thank you! I doubt I’d remove Cloudflare as i am now using it as an image cdn.

Plus in a few weeks I’ll probably remove the redirect after google reindexes.