Using htaccess, how to restrict seeing directory contents, but allow server to use the contents?

后端 未结 11 1949
既然无缘
既然无缘 2021-01-30 22:38

More specifically, I have an htaccess file that restricts anyone from seeing the directory contents. Such that, nobody can see my 1000s of images in www.example.com/images by us

11条回答
  •  渐次进展
    2021-01-30 23:17

    Option 1 (Easy but not Preferable)-

    You do not need to create an index file like this-

    
      Forbidden!
      
        

    Access Denied !!

    And place it in each resource directories and directories you don't want to show users.

    Option 2 (More Preferable)-

    You can just add this at the end of your .htaccess file-

    Options -Indexes
    

    So, your .htaccess file may be like this-

    
        
            Options -MultiViews
        
    
        RewriteEngine On
    
        # Redirect Trailing Slashes If Not A Folder...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)/$ /$1 [L,R=301]
    
        # Handle Front Controller...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
    
        # Handle Authorization Header
        RewriteCond %{HTTP:Authorization} .
        RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
        Options -Indexes
    
    

    If you do that and try to go to a directory, you would get something like this-


    So, no one will be able to discover any directory in server.

    More can be found here.

提交回复
热议问题