When we enter the URL of the website, it automatically catches up the index.html page or home.html or something else based on the default settings, why this happens? It is because these pages are defined as a default page for the root (public_html or WWW) directory.
You may want to redirect user to some other page instead of index page then in such case you can specify that page as a default page for root directory in .htaccess file. Let’s see how we can do it.
Suppose you want to display mypage.php page when someone tries to access the root directory (i.e. http://www.your-website-name.com/) then you just need to add the below piece of code to the .htaccess file.
Lets make it more versatile, suppose you have a 4 pages such as firstpage.htm, secondpage.htm, thirdpage.htm & fourthpage.htm and you wanna display second page if first page is not available, third page if second page is not available & so on. Let’s write it –
DirectoryIndex firstpage.htm secondpage.htm thirdpage.htm fourthpage.htm
» In this case secondpage.htm would be displayed if the directory doesn’t have firstpage.htm.
» thirdpage.htm would be displayed if firstpage.htm & secondpage.htm both don’t exist in the directory.
» So on…
How you can use this method to avoid directory listing?
In last post we learnt how we can avoid directory listing by modifying the .htaccess. We can also do the same using above discussed method. Let’s discuss it-
We have a situation wherein we don’t want a directory content listing for the directory – http://www.mysite.com/wp-content/uploads/
We are going to apply the above discussed method to fix this issue. We just have to create a .htaccess file and need to place that file in the above directory. Suppose the content of the file is –
Now, when anyone tries to browse the directory – [http://www.mysite.com/wp-content/uploads/], the index.html (home page) would be displayed to the user rather than the listing. This is one of the best to secure your website to avoid content theft.
Leave a Reply