• How To?
  • Tips ‘n Tricks
  • WordPress
  • Snippets
  • Software
    • Browsers
    • Downloads
  • Web
  • Tools
    • Character Counter
    • chmod Calculator
    • Entities Encoder
    • Live HTML Editor
    • My IP
  • Contact
Twitter Facebook Google+ RSS
You are here: SumTips » Tips 'n Tricks » Prevent Access to Files on Web Server using htaccess

Prevent Access to Files on Web Server using htaccess

Posted on January 17, 2011 by Renji | Short URL: http://sumtips.com/?p=2969

In this post I will show how you can prevent public access and protect a certain file on your web server or completely block access to a certain file type using the .htaccess file (Hypertext Access file).

Protect a certain file on your server

By using the below code you can prevent public access to any file you want. For example to prevent access to “file.jpg”  use the following code:

<files file.jpg>
order allow,deny
deny from all
</files>

Using this method you can protect the .htaccess file too.

<files .htaccess>
order allow,deny
deny from all
</files>

Now if anyone try to access that file, they will get a 403 error.

Prevent access to a certain file type

<Files ~ "\.log$">
order allow,deny
deny from all
</Files>

The above code prevents access to all .log files.

Prevent access to multiple file types at once:

<FilesMatch "\.(log|ini|htaccess)$">
order allow,deny
deny from all
</FilesMatch>

The above code prevents access to all files with .log, .ini and .htaccess extension.

Add extension of those files you want to prevent access to between “(” and “)” and separate each file extension with a pipe – “|.”

You can also allow or deny access to a file based on an IP address or block/range.


Tweet

Related posts:

  • Allow / Block Access to Site Based on IP address
  • Force Download Files on your Website using htaccess
  • Prevent Accidental Deletion and Modification of Files On USB
  • Access Linux Drives and Partitions from Windows
  • Prevent a USB Drive from Running Automatically
Categories: Tips 'n Tricks | Tags: htaccess, Tips 'n Tricks
Allow / Block Access to Site Based on IP address
Hide WordPress Update Nag in Admin Dashboard

  • Get Updates via Email

  • Recent Posts

    • CMS2CMS: Migrate Site from Drupal or Joomla to WordPress
    • WordPress: Add Preview Button in Distraction Free Writing
    • How to Activate or Deactivate Individual Jetpack Modules
    • Windows 8: Auto Update Defender with Windows Update Disabled
    • Automatically Start and Close Programs at Specific Time
    • How to Copy Code from Notepad++ with Syntax Highlighting
  • Random Posts

    • Run Multiple Websites in WAMP by creating Multiple Virtual Hosts
    • Upload Files to Multiple File Hosting Services
    • Display Simple Maintenance Mode Message to Non-Admins
    • Monitor GPU, CPU, Hard Disk Temperature, Set Temperature Alerts
    • Now Visualize your Friendship on Facebook with Friendship Pages
    • Online Tools to Check If A Game Would Run On Your Computer
  • Categories

    • Blogging
    • Games
    • Google
    • How To?
    • Linux
    • Microsoft
      • Windows
    • Miscellaneous
    • Phone
    • Snippets
      • AutoHotkey Snippets
      • CSS Snippets
      • htaccess Snippets
      • JavaScript Snippets
      • PowerShell Snippets
      • WordPress Snippets
    • Social
    • Software
      • Browsers
    • Tips 'n Tricks
    • Wallpapers
    • Web
© SumTips. Contact | Sitemap | Privacy Policy