user1687533
user1687533

Reputation: 43

redirect image to different image htaccess

I have tried cpanel redirect but it is not specific. I made a change to my site where anyone using a now old image needs to show a new image. I would like to redirect the link to the old image to the url to the new image.

I have an image http://mysite.com/images/image.png

When the image is called I want to show http://mysite.com/images/image.gif

What do I add to htaccess to create this specific rule?

Note that image1 is a png and I am replacing it with a gif so I cant just overwrite with the new image. This is why I am looking for a redirect solution.

EDIT: I can give the image the same name, it will just have different extension.

EDIT 2: change to show image would have the same name, different extension.

Upvotes: 0

Views: 5987

Answers (2)

Charlie Walton
Charlie Walton

Reputation: 322

A simple URL rewrite should do the trick!

Options +FollowSymLinks

RewriteEngine On
RewriteRule ^images/([a-zA-Z0-9]+).png$ images/$1.gif

Whatever is entered here '([a-zA-Z]+)' will be entered here '$1'. Hope that makes sence. Let me know how you get on!

(Note: there is no rewrite condition to check if the file exists because you want the url rewritten whether there is a file there or not)

Edit: Probably best to put a .htaccess in the images directory, in this case you can miss out the file paths as you are already in the directory.

Upvotes: 1

goodeye
goodeye

Reputation: 2459

If you're looking to replace this one specific image1.png to image2.gif, then this rule should work:

RewriteRule ^images/image1.png$ images/image2.gif [NC,L]

If you're looking for something more general-purpose, then it depends on your image naming.

Upvotes: 0

Related Questions