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:

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

Recent Posts

Securing phpMyAdmin Like a Pro: Essential Tips and Tricks

Securing phpMyAdmin is crucial to prevent unauthorized access and protect your databases. Here's a guide…

5 months ago

Pasqal raises $100M to build a neutral atom-based quantum computer

Pasqal, a Paris-based quantum computing startup, today announced that it has raised a $100 million…

1 year ago

Apple in talks with Disney, others on VR content for new headset: Report

Developed with Sony Group Corp, the headset will have two ultra-high-resolution displays to handle the…

1 year ago

Microsoft, Amazon results to highlight softening cloud business

After years of blistering growth, most recently fuelled by remote working and studying during the…

1 year ago

Intel chairman Omar Ishrak steps down

Omar Ishrak had stepped down and the chipmaker appointed board director Frank Yeary as his…

1 year ago

Canada to commercialise world's first photonic-based quantum computer

Canadian Prime Minister Justin Trudeau has announced a new federal investment to build and commercialise…

1 year ago