set up 301 redirect within wp-content/uploads/ directory of a WP site to a new image URL

If this image does not exist then the request will be rewritten to the WordPress front-controller before your redirect occurs.

Try the following instead using mod_rewrite at the top of the .htaccess file, before the # BEGIN WordPress code block:

RewriteRule ^(wp-content/uploads/2021/09)/image-1\.png$ https://%{HTTP_HOST}/$1/image-2.png [R=302,L]

The $1 backreference in the substitution string simply avoids having to repeat the full URL-path. ($1 contains wp-content/uploads/2021/09 – captured from the RewriteRule pattern.)

Note that this is a 302 (temporary) redirect. If this is intended to be permanent then change to a 301, but only once you have tested that it works as intended in order to avoid potential caching issues.


Alternatively, you can create an additional .htaccess file at /wp-content/uploads/2021/09/.htaccess – which will override the parent .htaccess file and allows you to “simplify”*1 the directive. For example:

# /wp-content/uploads/2021/09/.htaccess

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/(.+)/[^/]+$
RewriteRule ^image-1\.png$ https://%{HTTP_HOST}/%1/image-2.png [R=302,L]

The %1 backreference contains the URL-path leading to the image file (captured from the preceding CondPattern).

(*1 Ok, that’s debatable.)


UPDATE: to clarify 404 nginx

tl;dr you’ll probably need to contact your host (WPEngine) to get to the bottom of this.

You could either have one of two scenarios (probably the second). Either:

  1. You are on an Nginx server, so .htaccess files do not apply. You would need to perform this redirect in WordPress itself (eg. using a plugin). However, the plugin(s) you have tried do not appear to be working (I assume you are getting the same 404 nginx response), which leads me to believe it you have #2

  2. You are on Apache, but there is an Nginx front-end proxy sitting in front of Apache that is configured to serve static content (from /wp-content/uploads). The request for these resources (or this area of the filesystem) is handled entirely by Nginx. The request does not even reach Apache/WordPress, which is why you are seeing an Nginx generated 404 response.

    The Nginx proxy needs to be configured to allow requests through to Apache so you can implement the redirect (or the redirect would need to be implemented directly in the Nginx server config, which you probably don’t have access to?). This probably requires a support request with the host.