WordPress.org

Ready to get started?Download WordPress

Forums

How to change url of all images in wordpress media library? (4 posts)

  1. marcomilic
    Member
    Posted 6 months ago #

    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?

  2. bcworkz
    Member
    Posted 6 months ago #

    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.

  3. marcomilic
    Member
    Posted 5 months ago #

    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)$  http://www.loremipsum.com/wp-content/uploads/no-image/no-image.jpg [R=301,L]
    </IfModule>

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

  4. bcworkz
    Member
    Posted 5 months ago #

    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!

Reply

You must log in to post.

About this Topic