title: Forbid to visitors directories access by URL and redirect to a specific page without blocking the internal access when the website needs them?
body: <p>I got a problem and I almost fixed it with several solutions but never entirely, there is always a problem.</p>
  <p>I'm in a similar situation as the person in this topic: <a href="https://stackoverflow.com/questions/53050720/how-to-forbid-direct-access-to-website-directories-in-browser-url-but-allow-whe">topic</a> except I also have a Rewrite Rule ^([a-zA-Z0-9-_/]*)$ index.php?p=$1 in my htaccess.</p>
  <p>Problem ? The solution described in the topic &quot;Options -Indexes&quot; works, but not with my rewrite rule. Of course it seems to block the access but it gives me the Chrome &quot;Too many Redirects&quot; Error and I want to have in this situation my 404 error page. When I try for example to access my &quot;views&quot; directory I got the error I described just before and it seems to switch and redirect indeed between http://localhost/website/views and http://localhost/website/views/?p=views.</p>
  <p>I have some ErrorDocument rule but it doesn't seems to work in this case, and it seems like in the first place the rewrite rule doesnt work because It feels like it doesn't pass in my index.php where I'm composing with the $1 of the rewriten rule. There's no code in the details of the browser request, the status seems to be &quot;failed&quot; or &quot;canceled&quot;.. I'm working with Wamp and Apache for now. I'm trying to find a solution, do yo have any idea ?</p>
  <p><strong>EDIT:</strong>
  In My .htaccess I have:</p>
  <pre><code>Options +FollowSymlinks
  
  RewriteEngine on
  
  RewriteRule ^$ index.php [L]
  
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  
  RewriteRule ^([a-zA-Z0-9\-\_\/]*)$ index.php?p=$1 
  ErrorDocument 404 /website/views/404.php
  
  &lt;files .htaccess&gt;
  order allow,deny
  deny from all
  &lt;/files&gt;
  
  Options -Indexes
  
  DirectoryIndex disabled
  
  </code></pre>
  <p>And from a previous topic I got this in httpd-vhosts.conf :</p>
  <pre><code>&lt;VirtualHost *:80&gt;
    ServerName localhost
    ServerAlias localhost
    DocumentRoot &quot;${INSTALL_DIR}/www&quot;
    &lt;Directory &quot;${INSTALL_DIR}/www/&quot;&gt;
      Options +Indexes +Includes +FollowSymLinks +MultiViews
      AllowOverride All
      Require all granted
    &lt;/Directory&gt;
    ErrorDocument 403 /website/views/404.php
  &lt;/VirtualHost&gt;
  </code></pre>
  <p>In my website, with the rewrite rule, everything after /website/ in the url is considered as $1 and so requests are going through my index.php where I break down the $1 in parameters to get the proper controller and function. For example: with /website/article/read/1, $1 will be 'article/read/1' and then I break it down. All of this works perfectly.</p>
  <p>Now I'm trying to get url access more secure AND I want to display my own error page &quot;404.php&quot; to all url that end with an error.
  The thing is that when I try to access directories in url like &quot;views&quot; or &quot;controllers&quot;, I got the error I said in my first post, and I don't know how to display my error page.
  Now I have tried different things , some working more or less but I'm still waiting to find a solution. One thing I tried before and was the closer to a solution was to add &quot;DirectorySlash off&quot; to the .htaccess and it worked because I guess the directories were not considered as directories anymore. But I've seen that it was not secure and not a good thing to deactivate the &quot;DirectorySlash&quot; so I'm searching for something else now.</p>