Home > Blockchain >  Htaccess replace one image with another one
Htaccess replace one image with another one

Time:03-27

So Ive been making a code which replaces one image with another without changing the link.

So heres the code that I found on one of the forums.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^.*/fredShip1.png$ /fredShip2.png [L]
</IfModule>

So this code not only redirects user to another page but also to a random link. So original link is http://toss.rf.gd/storage/fredShip1.png though it should have replaced the image with http://toss.rf.gd/storage/fredShip2.png(Just an example) but it sends the user here toss.rf.gd/home/vol8_1/[Account info]/htdocs/storage/FredShip2.png

I added the image too -> The image

I am really bad at htaccess so make sure correct me if Im wrong. Also english is not my first language so expect some minor mistakes.

EDIT : So i solved the problem with redirection to a random link. But Im still wondering is it possible to just change the image without changing the link?

CodePudding user response:

RewriteRule ^.*/fredShip1.png$ /fredShip2.png [L]

The code you've posted already does essentially what you require, except that you need to adjust the paths to match your example. The "problem" with the above rule is that it rewrites the request to /fredShip2.png (in the document root), not /storage/fredShip2.png as in your example.

Assuming the .htaccess file is in the document root of the site and you wish to internally rewrite the request from /storage/fredShip1.png to /storage/fredShip2.png then you would do it like this:

RewriteRule ^storage/fredShip1.png$ storage/fredShip2.png [L]

There should be no slash prefix on the URL-path in either argument.

If you have other directives in your .htaccess file then the order of these directives can be important.

Make sure you've cleared your browser cache before testing.


but it sends the user here example.com/home/vol8_1/[Account info]/htdocs/storage/FredShip2.png

That's not possible with the directive you've posted. This is most likely a cached redirect due to an earlier (erroneous) experiment with 301 (permanent) redirects. For example, something like the following would produce the above "erroneous" output:

RewriteRule fredShip1\.png$ storage/FredShip2.png [R=302,L]

Note the use of the R (redirect) flag and the lack of a slash prefix on the RewriteRule substitution string (2nd argument). Since the substitution string is "relative", the directory-prefix (ie. /home/vol8_1/[Account info]/htdocs/ in your example) is prepended to substitution and since this is an external redirect (as denoted by the R flag) this then exposes the absolute filesystem path to the user.

NB: The above is a 302 (temporary) redirect - so should not be cached by the browser (at least not by default).

  • Related