Prevent Direct Linking

Discussion in 'Apache' started by proph3t, Apr 4, 2006.

  1. #1
    I run a flash arcade site and today while checking my stats I noticed that someone has been linking directly to one of my games, as in:

    http://example.com/game.swf

    Rather than:

    http://example.com/game/game-title/

    I am trying to figure out a way to prevent this from happening as this obviously hurts my revenue in that users can view the content without my site. Not only this, but it puts a strain on my bandwidth that is not being covered by my ads.

    The only way I can think of preventing this is somehow mod_rewriting all .swf files to, say, gamefile.php?file=(filename) and then using a script that will redirect to the actual page that corresponds to that .swf. This brings up a problem though in that the games on my site won't show anymore (gets into a loop of sorts).

    If I could code a RewriteCond that excludes the above rule from redirecting .swf files when the domain contains http://example.com/game/ then everything would work perfectly. Is there some way to do this?

    Is there an easier way than this even?

    Thanks in advance.
     
    proph3t, Apr 4, 2006 IP
  2. proph3t

    proph3t Guest

    Messages:
    145
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Ah, I believe I got it working:

    RewriteCond %{HTTP_REFERER} !game/

    I will report back on the success of this.
     
    proph3t, Apr 4, 2006 IP
  3. BILZ

    BILZ Peon

    Messages:
    1,515
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    0
    #3
    A fun thing to do is...Monitor this type of behavior and when you encounter someone hotlinking to one of your games - change the filename of the game and replace the old filename with an advertisement for your site that links to the page with the game. Then you get some free advertising. Plus, in the new ad you can say something about how that site likes to steall content from other sites.
     
    BILZ, Apr 4, 2006 IP
  4. proph3t

    proph3t Guest

    Messages:
    145
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Alright, after much work I have come up with:

    
        # Redirect from direct linking
        RewriteCond %{REQUEST_FILENAME} .*swf$ [NC]
        RewriteCond %{HTTP_REFERER} !game/
        RewriteRule (.*)(\.swf$) /gamefile/$1/ [R=302,L]
    
        # Prevent hotlinking
        RewriteCond %{REQUEST_FILENAME} .*swf$ [NC]
        RewriteCond %{REQUEST_FILENAME} !hotlink\.swf$ [NC]
        RewriteCond %{HTTP_REFERER} !^$
        RewriteCond %{HTTP_REFERER} !localhost [NC]
        RewriteCond %{HTTP_REFERER} !google\. [NC]
        RewriteCond %{HTTP_REFERER} !search\?q=cache [NC]
        RewriteRule (.*) /media/games/hotlink.swf [R=303,L]
    
    Code (markup):
    I am using localhost for now to test it.

    Unfortunately, while this filters *both* hotlinking and direct linking, it does not work perfectly. If I type the direct link to a game in the address bar it still appears, and only after a refresh will it redirect to the /gamefile/ page. I'm not sure why, but it defeats the purpose of the filter. Any ideas on how to make it work the first time anyone loads the page?

    The other problem is that the filtering for hotlinking and direct linking get in the way of each other. I need a way for the direct linking to only come into effect when someone is actually on my site, but *not* when they are loading the file from another site. For some reason:

    RewriteCond %{HTTP_REFERER} ^localhost$

    Does not work.

    So, I need a way to make them not interfere and also a way to have the direct linking prevention work the first time its loaded. Any help is greatly appreciated.
     
    proph3t, Apr 4, 2006 IP
  5. proph3t

    proph3t Guest

    Messages:
    145
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hmm, seems the issue with it not working the first time its loaded is actually an issue with caching. If I clear my cache and visit it works perfectly. After that it doesnt unless I refresh.

    Any way to prevent caching of .swf files?
     
    proph3t, Apr 4, 2006 IP
  6. tenhayo

    tenhayo Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I tried modifying the script you put up but i was unsuccessful with using it. I'm very new to HTACCESS can you walk me through with what i need to do to use this htaccess script on my site.

    Thanks
     
    tenhayo, May 15, 2006 IP