Page 1 of 1

What Is The Best Way To Deny Access To Directories?

PostPosted: 23. January 2018 00:07
by DeveloperDan
Hello everyone! I am new to the forums and looking to solve a problem. My apologies in advance if I am not asking my question in the proper location. I will do my best to explain my problem in such a way that supplying an answer should be simple. At least I hope. :) :)

____________________________
Example Directory Structure:
____________________________
root/
/js/
/css/
/images/
/new_images/
/old_images/
/misc/
/misc_one/
/misc_two/
index.php
about.php
contact.php
.htaccess
robots.txt
____________________________

The above is my example of a simple file structure 3 levels deep. (E.g: root/images/new_images). My actual website is a WordPress website and file structure.

What do I want to achieve or prevent? I want to stop anyone from being able to directly access a directory and view its contents by simply visiting the directory path. I know I could drop an index.php/HTML file in each directory but there are just too many, some directories are created dynamically and it's just not practical. When I visit mywebsite.com/images/ or www.mywebsite.com/misc/misc_two or any other directory with no index file in it I want to either redirect the user to a specific page or show them nothing. So long as I have control of what they see or don't see.

The Short Version Of My Question:
-----------------------------------------
How do I keep people from viewing the contents of any and all the directories on my website using the .htaccess file? Is there a directive to redirect visitors to another page once they've manually entered into a directory with no index file? I do not want anyone to be able to look into a directory. Keep ni mind I am using this on a wordpress website.

As always, thank you all for reading and I hope to hear from you soon!

Regards
DeveloperDan

Re: What Is The Best Way To Deny Access To Directories?

PostPosted: 23. January 2018 00:52
by Altrea
Hi DeveloperDan,

Short Answer: Wordpress already comes with a predefined .htaccess file which uses a Front Controller Pattern to generate SEO-URLs.
https://codex.wordpress.org/htaccess

best wishes,
Altrea

Re: What Is The Best Way To Deny Access To Directories?

PostPosted: 23. January 2018 22:40
by DeveloperDan
Thank you very much for your answer. Kind regards!

Re: What Is The Best Way To Deny Access To Directories?

PostPosted: 24. January 2018 11:46
by Nobbie
Just in case you are interested in a solution without wordpress:

The easiest solution is identical to the default installation, deny access to the root folder (Require none) and then apply permission to any file/folder that may be read (Require All Granted).There is also the option if installing a sophisticated URL rewrite mechanism, or apply Rights in conjunction with file pattern (<FilesMatch...) or Locations (Location matches against the URL, where File matches against the physical path). There are lots of different options to do so, it requires a little analysis in order to determine which way suites best.

WIth the help of ALIAS you also have the option to put files and folders to pathes somewhere else. There is also "Option -Indexes", which deny directory listings. And and and .... there are plenty of solutions. But of course, it requires some knowledge in Apache configuration, what you cannot learn in a few hours. Apache configuration is a demanding task and requires a lot of effort.