SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Rewrite rule for blocking hot-link

    I would appreciate help in clearing up a rewrite rule for blocking hot-linking.

    This works --

    Code:
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?mysite\.(net|com)/.*$ [NC]
    RewriteRule .*\.(jpg|txt|css|js)$ / [R,NC,L]
    -- to redirect any external direct calls to my pictures, text files, style sheets, or scripts to my home page.

    With this rule, my own pages can access all those files, except for one -- my favicon, which is here:

    Code:
    /res/favicon.jpg
    My .css and .js files are in that same /res/ directory, and are called from the same headers that call the favicon, and they all work with this rewrite rule. Only the favicon is blocked.

    Can someone spot the error and show me how to allow the favicon to be accessed by my own pages?

    Thank you.

  2. #2
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have the same question as you, I have found my host only allows a selection of htaccess rules, which is why I think mine didn't work.

    The code I used was pretty much the same as yours. I would send an email to your host provider asking if these rules are blocked.

    Si

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

    What you need is to FAIL an invalid request ...
    Code:
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?mysite\.(net|com)/.*$ [NC]
    RewriteRule \.(jpg|txt|css|js)$ - [F]
    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

  4. #4
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    265
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    If the problem is favicon.jpg, try use an GIF, or PNG image for favicon.
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  5. #5
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Responses

    Thanks for all these responses.

    DK – I tried those lines of code, and still get the same result: all the jpg|txt|css|js files are blocked from direct call, and still show up as expected when called by my own pages – except the favicon, which my own pages can’t access using this code either. Mysterious. I’ve checked and double-checked – same result.

    MarPlo – Using a gif for the favicon doesn’t solve the problem when I add gif to the filter (jpg|txt|css|js|gif), though it could conceivably be a kludge, since I have no other gifs in my whole site and thus could allow direct calls to gif (the number of hotlink calls to my favicon likely being, er, small).

    *

    Surprise! – It’s a browser problem. I should have thought of this sooner. I mainly use Mac/Firefox.3.6 and that’s where I get the problem. But Safari and iCab display the favicon as expected with either form of the filter (my original and DK's variant). This obviously isn't a huge problem but if anyone might suggest why this would happen I'd appreciate knowing, just for the sake of thoroughness. (I could try a new Firefox but prefer to stay for now with 3.6).

    I really appreciate the help in this forum.

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,804
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by MarPlo View Post
    Hi,
    If the problem is favicon.jpg, try use an GIF, or PNG image for favicon.
    Or better yet make it an icon (as the name indicates that it is actually supposed to be) - favicon.ico
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,650
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Bingo! Stephen to the rescue (again)!

    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

  8. #8
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thank you

    Thank you!

    This forum is so helpful.


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
  •