Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache htaccess: Block direct access to index.html in a folder

I have the following code in my .htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On

    # block text, html and php files in the folder from being accessed directly
    RewriteRule ^content/(.*)\.(txt|html|php)$ error [R=301,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule . index.php [L]
</IfModule>

# Prevent file browsing
Options -Indexes

To block accessing txt, html, php files in the content folder. It works great and it blocks index.html if access the following URI

mysite.com/content/index.html

but it's not blocking the index.html content being displayed if it's omitted from URI like:

mysite.com/content/

How to solve this problem? Thank you.

like image 860
user702300 Avatar asked Nov 28 '13 01:11

user702300


People also ask

How do I block direct access in HTML?

You can define a variable like window. parentPage = true; in the index. html file.

How do I restrict access to a folder in PHP?

By default, PHP does not restrict which files and directories your PHP scripts can access. To restrict the directories that can be accessed, you can use PHP's open_basedir setting.


1 Answers

You need to disable the directory index, not blocking anything. Just add this to your .htaccess file:

DirectoryIndex none.none
Options -Indexes

First line is to tell apache not to serve the "index.html" in case of a user navigates to the folder. More info at DirectoryIndex doc.

Second is to tell apache not to show the content of the folder, because there is no default file to show (First line disable it)

like image 159
Babblo Avatar answered Oct 19 '22 19:10

Babblo