Support » Plugins » Hacks » How to change url of all images in wordpress media library?

  • Hello,

    I am working on large website (to many images, posts, large database), and I need to change almost every url of image in wp-content/uploads/ to target a one specific image. I have tried with WP RegEx Replace plugin, using this instructions but it’s not working. When I try to change specific url of one image it work, but if I want to select all images using wp-content/uploads/(.*)/(.*).jpg it’s not working.

    Do you have idea how to do this?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz


    Probably the easiest way is to use .htaccess and mod-rewrite to serve a particular image regardless of which image is requested. Since regular expressions are fully supported, you should be able come up with the needed directives to capture the needed images and pass the remainder.

    Tnx for replly,

    Yes, that was my first idea, I edited .htaccess like this

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    RewriteRule   ^/uploads/(.*)/(.*)/(.*).(png|jpg|gif)$ [R=301,L]

    But is not working, images url are not changeing. I read few instructions, but I’m not sure where is mistake?

    Moderator bcworkz


    The mistake is adding your rule to the default WP block. You need to write your own block above the WP one, since there is little chance of anything below the WP one of getting executed. It can be in the same ifmodule block, but needs its own set of engine on, rewritebase, rewritecond and rewriterule.

    The RewriteCond is where the regexp goes, and if that’s a match, only then is the RewriteRule executed, which should basically rewrite anything to the image path. There’s no reason to match anything in rewriterule unless you need to use that match as part of the new path.

    I’m intentionally not providing a code example because one silly little error in this file can crash your entire site, and I don’t want to be responsible for that. I’m hoping you can figure this out from here without an explicit example. Good luck!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to change url of all images in wordpress media library?’ is closed to new replies.