Deny Access to a Directory or File with .htaccess
Have your website all setup? Amazing! There might be a directory that you want no one to ever visit, not even yourself! Although this does not really make sense on a web server, here are the instructions to make it happen. Please note that the visitor must have access to the file for it to be shown. For example, if you block your folder with all the images, no images will show on your site. If you only want to block others out, but allow yourself access, learn how setup that here.
All you have to do is add this code into your .htaccess file and save it! Like all file changes, it can take some time before it updates everywhere, but you can speed up the process on your devices simply by clearing your browser’s cache. This code snippet will block all access to a directory or file!
Whatever folder you put this htaccess file in, will have access to it blocked. For example, if you put it in a folder called "text", someone trying to access that page will get an error. If you put this code in your root folder, no one will be able to access your website. If you want to block access to a file, please the file and this code into its own seperate folder.
deny from all
Unsure of how to install this code? It's pretty simple. A .htaccess file is called “.htaccess”, with nothing before the dot. Try finding this on your FTP software. Many systems don’t allow filenames starting with a dot (So downloading a htaccess file can be difficult at times), so that also restricts how we can create a .htaccess file. It is possible to get around these restrictions though, so here are the steps!
- Open your text-editing program and code your .htaccess file
- Once you are finished, save it as “htaccess.txt” (We will fix its name later)
- Upload “htaccess.txt” to your webserver and rename it to “.htaccess” (Remember that the dot is important!)
If you are still confused, or it's not working for you, check out the more detailed instruction in our Finding, Creating, and Editing a .htaccess file article.
Hopefully you were successful in installing this snippet, if you need help, feel free to send us a message! Check out the rest of the htaccess snippets to see what other amazing things you can do!