BrunoLM
BrunoLM

Reputation: 100381

How to use htaccess to change image paths?

I have a html file that has an image:

<img src="../smilies/happy.gif" />

Is it possible to redirect this path to another using .htaccess?

So the above example would become equivalent to

<img src="http://static.example.com/smilies/happy.gif" />

Upvotes: 2

Views: 8109

Answers (3)

adarshr
adarshr

Reputation: 62613

RewriteRule ^(smilies/.*)$ http://static.example.com/$1 [R]

Upvotes: 0

Ren&#233; H&#246;hle
Ren&#233; H&#246;hle

Reputation: 27325

I think its easier to set a variable in your config

<?php 
$static_url = "http://static.example.com/";
?>
<img src="<?php echo $static_url; ?>/smilies/happy.gif" alt="" />

Its better then you have the control what came from static and the rewrite engine is slow when you call it everytime its not sooo good.

Upvotes: 1

Nanne
Nanne

Reputation: 64429

Well, you might get away with redirecting all calls that end with /smilies/happy.gif to that directory, so it wouldn't matter where the call came from. That does mean you should not wish to call this to some other subedirectory, but I imagine you don't.

something like this (guessing here, cann't test, so read up on the rwriting there :)

RewriteRule     ^(.*)/images/$(.*) http://static.example.com/smilies/$2 [L,R=301]

basically you are rewriting everything that has '/images/' in it to that static adress, pasting whatever was after images after the new asdress (the $2 thingy) and then indicating that this is the last command to parse (to stop strange things in the htaccess) and that you want a 301 (permanently moved) code to be sent.

Upvotes: 5

Related Questions