1

I need to do a redirect within wp-content/uploads/ - to a new image name, because the old one doesn't work. How do you do this?

I want to redirect from https://example.com/wp-content/uploads/2021/09/image-1.png to https://example.com/wp-content/uploads/2021/09/image-2.png - I tried using both of our 301 redirect plugins (Yoast, and Redirection) but neither worked.

I also tried changing .htaccess to this from (https://wordpress.org/support/topic/htaccess-cant-redirect-file-under-wp-content/):

# ...stuff above

# END WordPress

RedirectMatch 301 (?i)/wp-content/uploads/2021/09/image-1.png https://website.com/wp-content/uploads/2021/09/image-2.png

# ...stuff below

But it didn't work.

How do you do this?

9
  • I wonder what it means that the old image doesn't work
    – birgire
    Sep 14, 2021 at 17:58
  • And what do you mean exactly when the redirect "didn't work"? What response do you get? Any error? A WP 404 response? An Apache 404 response? Or something else?
    – MrWhite
    Sep 14, 2021 at 18:45
  • to clarify 404 nginx
    – kawnah
    Sep 14, 2021 at 18:45
  • "nginx"?! How does Nginx fit into your config? Nginx does not use .htaccess. And to clarify, the original image (ie. image-1.png) does not exist on the filesystem?
    – MrWhite
    Sep 14, 2021 at 19:19
  • idk sorry I'm mostly Front End I don't really do or understand server side.
    – kawnah
    Sep 14, 2021 at 19:20

1 Answer 1

1

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.

2
  • Thanks for commenting and the assist but this doesn't work sorry
    – kawnah
    Sep 14, 2021 at 19:01
  • I still get a 404
    – kawnah
    Sep 14, 2021 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.