SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    htaccess - Blocking all but known file extenstions

    I was wondering if someone could give me an example of how to block all but a few specific file extensions using htaccess?

    Anything except jpg, css and js need to be blocked.

  2. #2
    SitePoint Member tquizzle's Avatar
    Join Date
    Jun 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Havent seen it done that way, but its easy to block specific file types. Perhaps you could block all file types except jpg, css and js.

    Code:
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com/.*$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourotherdomain.com/.*$ [NC]
    RewriteRule \.(gif|jpg|jpeg|png|js|css)$ - [F]
    The above code is almost the opposite than what you need, but easily configurable. the "yourdomain.com" are domains that dont have to adhere to the rule.
    So basically, you could use this code but change the RewriteRule section to include the file extensions you dont want to be accessed.
    Don't waste time doing 'things right', just do the 'right things'

    TQuizzle.com

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tquizzle View Post
    Havent seen it done that way, but its easy to block specific file types. Perhaps you could block all file types except jpg, css and js.

    Code:
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com/.*$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourotherdomain.com/.*$ [NC]
    RewriteRule \.(gif|jpg|jpeg|png|js|css)$ - [F]
    Thanks for helping, but the code you posted is for more of a website level blocking. I would rather not use RewriteRules and try something like the FilesMatch or Files commands for Apache.

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

    Aw, tq was trying to help. In fact, the code displayed could have easily been modified to:
    Code:
    RewriteEngine on
    RewriteRule !\.(gif|jpg|jpeg|png|js|css)$ - [F]
    and it would do exactly what you asked for - INCLUDING blocking all .php and .html pages!Where'd you ever come up with a list like that?

    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


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
  •