Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I protect a credentials file using .htaccess

I am working on an old project.

The database credentials are stored as JSON in web root in a file like .my_secret and configurations are stored in .my_config – A PHP script reads these files and uses the data for connecting to the database and retrieving other settings.

If someone tries to access this file, I want to throw a 404 error, file not found.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I do this using only .htaccess? I don’t want to use .htpasswd

I tried to move the files outside the web root, but I encountered an open_basedir restriction in effect

>Solution :

You can simply create a rewrite rule for these files that points to a 404 file:

ErrorDocument 404 /404.php

RewriteRule ^(\.my_config|\.my_secret) 404.php [L,nc]

In the 404.php file you can output 404 headers like this:

<?php
header('HTTP/1.0 404 Not Found');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading