SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict mr.pita's Avatar
    Join Date
    Aug 2005
    Location
    USA
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Display watermark on images from with referrals from outside sources

    I have an image site of sorts and often get heavy traffic, not to the pages the images are on but to the images themselves. I've set up a script that has created copies of the original image with a small watermark that points them to my homepage.

    The problem is displaying this alternate image only when the traffic is coming from an outside source. I have the htaccess code and as far as I can tell it's all good, but when I set it loose, I get a 500 internal service error.

    This is what I have. The images are all in mydomain.com/images/ and the watermarked images are all in mydomain.com/images/watermark/. All the images are jpg's and all have a name that is a number.

    Code:
    RewriteEngine On
    RewriteCond %{HTTP_REFERER} !^http://([-a-z0-9]+\.)?mydomain\.com/ [NC]
    RewriteCond %{HTTP_REFERER} !^$
    RewriteRule ^images/(.*)\.jpg$ /images/watermark/$1\.jpg [L]
    The 500 error only comes on when the referrer is not my site. They display correctly when I go to the image directly for example. Am I doing anything wrong? Could this be a server configuration?

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,671
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    PITA,

    Your problem is the lack of knowledge of regex (and using the LAZY regex as a result). Do you not think that (.*) will match watermark/(.*), too?

    Change (.*) to ([a-z]+) or ([^/]+) and you'll avoid the loop.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Addict mr.pita's Avatar
    Join Date
    Aug 2005
    Location
    USA
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    D'oh! Can't believe I missed that.
    Thanks a bunch. I owe you my second-born (I promised my firstborn to someone else--I gotta stop using my future children as payment).

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,671
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    PITA,



    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    SitePoint Addict mr.pita's Avatar
    Join Date
    Aug 2005
    Location
    USA
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sooooo that worked great on that site, but as a matter of experimentation, for another site with a similar structure, I want to redirect to the page with the image on it.

    The problem is that right now the images have a space in their names and I don't know how to deal with that.

    The images are in mydomain.com/images/ and have the name structure "image (#).jpg" --the # represents a number; there are about 4000 images, all numbered. The page with the image is at mydomain.com/?i=# where # is the number that's in the image name.

    Assume the first 3 lines are the same. I tried the following, and get a 500 error:

    Code:
    RewriteRule "^images/image \(([0-9]+)\)\.jpg$" /?i=$1 [L]
    Not sure what I'm getting wrong here. Guess I'm not very good at this...

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,671
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Mr PITA,

    Why would anyone use quotes in a mod_rewrite statement? Escape brackets or ...? Please have a look at the tutorial in my signature.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  7. #7
    SitePoint Addict mr.pita's Avatar
    Join Date
    Aug 2005
    Location
    USA
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Idk I saw it in some other place that if there's a space you should use quotes around the whole thing. I shouldn't trust everything I read on the internet, but yea I'll take a look at that tutorial.

    Thanks

  8. #8
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,671
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    PITA,

    NEVER use quotes in mod_rewrite statements!

    If you have a space (i.e., %20) in a URL:

    1. Shame on you.

    2. Use [\ ] to specify the space character (to match the %20). Yes, that's an escaped space in the character range definition.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  9. #9
    SitePoint Addict mr.pita's Avatar
    Join Date
    Aug 2005
    Location
    USA
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good God, dklyyn, you are a genius.
    I bow down in praise.

  10. #10
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,671
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    PITA,

    On your feet, man!

    Okay, I'm glad that you got that resolved but may I recommend that you read the tutorial in my signature. I'm sure that I mentioned there that I use a str_replace(' ','_',$link) to replace spaces in the URI AND reverse that to reinstate the spaces for your db access. Example: http://wilderness-wally.com which uses (almost) ONLY titles of his articles for links. Easy-peasy!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  11. #11
    SitePoint Member
    Join Date
    Nov 2009
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wonderful !


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •