Is there a way to disable htaccess to website users? What I need is htaccess to disable only to a select group of my website users to access a directory full of uploaded images and to the non-selected user have htaccess enabled to stop them from accessing the directory!
Any help would be great!
You'll want to do this: Basically redirect all requests to a php file which then does the access control.
.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /CheckAuthorizedToFile.php?file=$1 [NC,L,QSA]
CheckAuthorizedToFile.php
<?php
$basepath = '/path/to/images/';
$realBase = realpath($basepath);
$userpath = $basepath . $_GET['file'];
$realUserPath = realpath($userpath);
if ($realUserPath === false || strpos($realUserPath, $realBase) !== 0) {
//prevent directory traversal by exiting execution
exit();
}
if($_SESSSION['IsAllowedToViewFiles']===true)
{
$file = $_GET['file'];
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
}
else
{
echo "Not Autorized please login.";
}
htaccess is managed outside of php (e.g. by apache environment) and is scanned and executed before the php script starts. So the answer is NO.
This is related to "Enable/Disable htaccess with PHP?" Apache itself can manage access control by users. But this is again outside of PHP control.
Possible PHP solution: Disable access of a directory by apache for all, and deliver content/files by php after user verification.