I have a directory structure “like” this
- Root
- Sub Dir A
- Content
- Admin
And I have some files in Admin that I need to .htaccess forbid. I know they should be outside the root but in this situation I cannot. What is the correct syntax for htaccess to redirect a direct request for say ‘Sub Dir A/Admin/abc.php’ (say a forbidden file) back to say Sub Dir A/Admin/index.php – just an example. To note “abc.php” may be an include so it should only be a direct request. Oh forgot to ask, which directory to put the .htaccess file in
If you really want to do this with mod_rewrite, then the rule can be:
This will issue 301 Permanent Redirect if you attempt to access
abc.phpdirectly.This is to be place in .htaccess in
/subdir_a/admin/folder.If you wish — you can place it in .htaccess file in website root folder — then alter rewrite rule a bit to include the path to
abc.php:Alternatively you can issue 403 Access Denied response:
The common practice is not to relay on .htaccess and mod_rewrite (that can be unavailable on some shared hosting plans or disabled for performance reasons) and use PHP-Script side checks:
1. To be placed in included/protected file (e.g. abc.php):
2. To be placed in calling/master script (that you can access directly):