2

I have a website, example.com

I also have sub-directories as under:

example.com/dir1/
example.com/dir2/
example.com/dir3/

Now, I am looking to redirect the visitor to 404 if they they try to access:

example.com/dir1/

But not in case, when they try to access :

example.com/dir1/example.php

Below is my .htaccess :

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ / [L,R=301]

Options -Indexes

with above htaccess i get a default error when i try to access any subdirectory as under:

Access forbidden!

    You don't have permission to access the requested directory. There is either no index document or the directory is read-protected.

    If you think this is a server error, please contact the webmaster.

    Error 403

    localhost
    Apache/2.4.16 (Win32) OpenSSL/1.0.1p PHP/5.6.12
  • ErrorDocument 404 /404.html you can write your folder name with or condition as we do to hide css/js folder ------------> RewriteRule ^(?:folderone|foldertwo|folderthree)\b.* index.php/$0 [L] – Monty Jan 25 '16 at 12:15
  • `RewriteCond` takes two parameters, not three. Your example makes no sense in that regard. – CBroe Jan 25 '16 at 13:06
  • @Starkeen: I count three – `%{THE_REQUEST}`, `/index\.php` – and ^[A-Z]{3,9}\ in the middle. – CBroe Jan 25 '16 at 13:43

3 Answers3

1

Try using this in your .htaccess file!


ErrorDocument 404 /filenotfound.html

IndexIgnore *
Aimal Azmi
  • 141
  • 7
0

Try :

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L,R=404]
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • if you have index it's still throwing 404 when you don't specify the page: http://website/ - will throw 404 http://website/index.php - will not throw 404 – Mortimer Feb 12 '20 at 16:25
0

I have made some research and found this:

Options -Indexes

from here.

It will throw 403 - Access forbidden which is better then auto-index with listed files.

Mortimer
  • 300
  • 5
  • 11