WordPress: How to change link to image source file instead of attachment link?

For several years I used a WordPress plugin called PhotoPress that was GUI to upload and place images in WordPress posts. The plugin was discontinued in 2007 as WordPress’s own Media gallery greatly improved.

The trouble that I now have is that four or five years worth of images in my posts were uploaded using PhotoPress which attached the images to posts in a particular way that is different from the native WordPress default.

Read More

When I switched to a new theme recently it caused a number of problems.

Looking at Google Webmastertools I have over 4000+ 404 errors due to bad links from this situation. (See URLs below)

Although images show up in the posts, if you click directly on any of the images in the older posts, instead of going to a page showing the image, it returns a 404 error. Please see the following:

The images appear correctly inside the post:

http://www.hongkonghustle.com/fashion/3138/thierry-mugler-hong-kong-china-fashion-show-western-market-hk-rick-genest-zombie-boy-rico-nicolas-formichetti-lady-gaga-stylist-joyce-boutique-store-skeleton-tattoo/

However, clicking on an individual image returns a 404 error.

The way that PhotoPress linked the image looks like this: /?pagename=album&?pp_album=main&pp_cat=default&pp_image=zombie_boy_tattoo_lady_gaga_rick_genest.jpg

Meanwhile, the directory path where the image source is actually stored is here:

http://www.hongkonghustle.com/wp-content/photos/zombie_boy_tattoo_lady_gaga_rick_genest.jpg

I’m not certain, but I think this is because the images are not linked to the source file directly – instead they are linked to the POST ATTACHMENT link.

I am very confused about how to fix this.

The images were originally (and still are) stored in wp-content/photos folder, so if you click the direct URL it works.

BUT, when I switched to my new theme, the images did not appear in the Media Library.

So I batch re-uploaded over 4000 images into the Media Library, and they are now sitting there unlinked.

I’m interested in a way of mass re-attaching the source images, and getting rid of the 404 errors.

How would I accomplish this?

Any thoughts? Your advice is greatly appreciated! Thanks!

Related posts

Leave a Reply

2 comments

  1. I ended up doing a 301 redirect in htaccess:

    Options +Indexes
    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond  %{QUERY_STRING}  ^pagename=album&??pp_album=main&pp_cat=default&pp_image=(.*)$
    RewriteRule .* /wp-content/photos/%1? [L,R=301]
    
    RewriteCond %{HTTP_HOST} ^hongkonghustle.com
    RewriteRule ^(.*)$ http://www.hongkonghustle.com/$1 [R=permanent,L]
    
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ http://www.hongkonghustle.com/$1/ [L,R=301]
    
    RewriteEngine On  
    RewriteBase / 
    <Files wp-config.php>
    Deny from all
    </Files>
    
    <Files wp-config.php>
    Deny from all
    </Files>
    Options +Indexes
    IndexOptions -FancyIndexing
    
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress
    
  2. How about doing a 301 permanent redirect on the new theme index.php whenever there is a pp_image query string?

    You could redirect

    http://www.hongkonghustle.com/?pagename=album&amp;?pp_album=main&pp_cat=default&pp_image=zombie_boy_tattoo_lady_gaga_rick_genest.jpg

    to

    http://www.hongkonghustle.com/wp-content/photos/zombie_boy_tattoo_lady_gaga_rick_genest.jpg

    by appending $_GET['pp_image'] to http://www.hongkonghustle.com/wp-content/photos/ ( content_url() ? )

    Just make sure that the redirection is done before output is sent, PHP output buffer could come in handy.

    Hope that helps.


    Possibly useful links: