Home > Software engineering >  Issue blocking & redirecting access to all directories on server using htaccess
Issue blocking & redirecting access to all directories on server using htaccess

Time:05-01

Essentially I would like to block access and redirect users to a page called test.com/redirect/page.php from all directories unless a specific file is visited.

For example I would like to block users and redirect from the following:

https://test.com/folder1/ should block with a 403 Forbidden error & redirect to test.com/page.php it should redirect because there is no index.php in that directory and no other file was specified in that url request.

https://test.com/folder1/page1.php is visited it should load no problem.

How can this done using the main htaccess file in directory.

CodePudding user response:

It sounds like you just need to configure a custom 403 error document. For example:

ErrorDocument 403 /redirect/page.php

There is no external "redirect". The 403 error document is served by an internal subrequest (the visible URL does not change) whenever Apache triggers a 403 response.

You should also ensure that directory listings (mod_autoindex) are disabled, so that a 403 is indeed triggered when a DirectoryIndex document is not present in the requested directory.

Reference:

CodePudding user response:

to redirect to homepage if the request is not for a file or dir, you can use the .htaccess

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ / [L]

Regards!

  • Related