Dynamic image location (URL)

Discussion in 'Programming' started by AHA7, Apr 25, 2007.

  1. #1
    Hello,

    I am looking for some script that will allow me to show different images to different people but using the same URL. OK, I think an example would be the best way to explain what I want to do, so here is an example.

    I have an image named "tech.jpg" and another one named "non-tech.jpg", now I want to use same URL, say: "http://mysite.com/img/mypic" and I want the script to return the first image "tech.jpg" if the request (user) came from (clicked the link from) Forums.DigitalPoint.com, otherwise the link would show the second image "non-tech.jpg". So how can I make the same URL show different images (I want the URL to return an image and NOT a page that contains the right image) to different people depening on the referring source?
     
    AHA7, Apr 25, 2007 IP
  2. Jim_

    Jim_ Peon

    Messages:
    72
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    A .htaccess file like this should work fine:
    RewriteEngine On
    RewriteCond %{HTTP_REFERER} !^http://(.+\.)?yoursite\.com/ [NC]
    RewriteRule !^(non-)(.*)\.jpg$ non-$1.jpg [L]
    PHP:
     
    Jim_, Apr 25, 2007 IP
  3. AHA7

    AHA7 Peon

    Messages:
    445
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for your reply, Jim!
    But could you please explain how that works? I am not familiar with the .htaccess file and rewrite rules, so if I want to apply this code to my example above, how should it look like?
     
    AHA7, Apr 25, 2007 IP
  4. Jim_

    Jim_ Peon

    Messages:
    72
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Enable mod_rewrite
    RewriteEngine On

    Set condition: Referer is not from mysite.com
    RewriteCond %{HTTP_REFERER} !^http://(.+\.)?yoursite\.com/ [NC]

    URL's not starting with non- and ending with .jpg, redirect to non-filname.jpg
    RewriteRule !^(non-)(.*)\.jpg$ non-$1.jpg [L]

    To make it work for your site, just change yoursite.com to your domain. Save it as .htaccess and put it in the folder you wish to have your images swapped on.
     
    Jim_, Apr 25, 2007 IP
  5. AHA7

    AHA7 Peon

    Messages:
    445
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    OK, so if I want to redirect URLs referred to my site from digitalpoint forums to "tech.jpg" and not "non-tech.jpg", do I use the following?

    RewriteEngine On
    RewriteCond %{HTTP_REFERER} http://(.+\.)?forums\.digitalpoint\.com/ [NC]
    RewriteRule (non-)(.*)\.jpg$ $1.jpg [L]
    Code (markup):
    I have some other questions please:
    1- What does [NC], [L] at the end of the second, third lines mean?
    2- Does the above redirect rules apply if the user came from a page deep in forums.digitalpoint.com, say "http://forums.digitalpoint.com/forumdisplay.php?f=15" or it just works if the user came from forums.digitalpoint.com?

    Thanks!
     
    AHA7, Apr 25, 2007 IP
  6. Jim_

    Jim_ Peon

    Messages:
    72
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    1)NC is case insensitive flag
    L is last rule flag

    2) Add a ^ before the http:// and it should work for anything on the domain forums.digitalpoint.com
     
    Jim_, Apr 25, 2007 IP
    AHA7 likes this.
  7. AHA7

    AHA7 Peon

    Messages:
    445
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks!
    Added some rep ;)
     
    AHA7, Apr 25, 2007 IP