How to deny/allow access to files using .htaccess file

Question:

How can I allow and deny HTTP access to files based on file extensions? I need to deny access to all TXT files.

Answer:

Below you can find a simple example of how to control download access to files using a .htaccess file. In the first example, the following .htaccess code will block access to all files with a .txt file extension for all requests resulting in HTTP ERROR 403: Forbidden.

<FilesMatch ".txt">
    Order Allow,Deny
    Deny from All
</FilesMatch>

If download access is required on the basis of the source IP address, it is possible to allow access on an IP basis. The following code will deny access to all of them and then allow access to the .txt file to requests coming from e.g. 11.110.299.255 IP address:

<FilesMatch ".txt">
    Order Deny,Allow
    Deny from All
    Allow from 11.110.299.255
</FilesMatch>

Recent Posts

Unlocking the Secrets of JSON.stringify(): More Than Meets the Eye

JSON (JavaScript Object Notation) is a lightweight data-interchange format widely used in web development. At…

2 months ago

How to Handle AJAX GET/POST Requests in WordPress

AJAX (Asynchronous JavaScript and XML) is a powerful technique used in modern web development that…

3 months ago

Page Speed Optimization: Post-Optimization Dos and Don’ts

Introduction After successfully optimizing your website for speed, it's essential to maintain and build upon…

3 months ago

Ultimate Guide to Securing WordPress Folders: Protect Your Site from Unauthorized Access

Securing your WordPress folders is crucial to safeguarding your website from unauthorized access and potential…

4 months ago

HTML CSS PHP File Upload With Circle Progress Bar

Creating a file upload feature with a circular progress bar involves multiple steps. You'll need…

4 months ago

Using WP Rocket with AWS CloudFront CDN

Integrating WP Rocket with AWS CloudFront CDN helps to optimize and deliver your website content…

4 months ago