I'm currently facing a little problem using .htaccess to redirect clients via "ephemeral" and unique links to different content (with different paths and extensions).
Let me explain this with some code I already got
.htaccess :
RewriteEngine on
RewriteRule (.+?)$ call.php?code=$1 [L,QSA]
call.php :
<?php
require_once("encryption.php");
if(isset($_GET['code']))
{
// decryption of the code
// the decrypted code is the content path
$clearCode = DecryptFunction(htmlspecialchars($_GET['code']));
if(strpos($clearCode, '.php') !== false)
{
include($clearCode);
}
else if(strpos($clearCode, '.jpeg') !== false)
{
$file_path_name = $clearCode;
$ctype= 'image/jpeg';
header('Content-Type: ' . $ctype);
$handle = fopen($file_path_name, "rb");
$contents = fread($handle, filesize($file_path_name));
fclose($handle);
echo $contents;
}
else if(strpos($clearCode, '.mp4') !== false)
{
$file_path_name = $clearCode;
$ctype= 'video/mp4';
header('Content-Type: ' . $ctype);
$handle = fopen($file_path_name, "rb");
$contents = fread($handle, filesize($file_path_name));
fclose($handle);
echo $contents;
}
}
?>
In most situations this code works great but can cause some performance issues when big files are requested (like videos) and use larges amount of RAM on the server side, so I need a more efficient way for redirect clients.
The best would be to make those redirections directly from the .htaccess file but I don't know how to retrieve the decrypted path from there and I'm not sure it's possible. Then I could use apache environment variables to store temporarily the encrypted paths (as var name) and the decrypted one (as value) and retrieve them in the .htaccess, that could be something like this :
<?php
require_once("encryption.php");
$filePath = "some/clear/path/to/encrypt.extension";
$encryptedPath = EncryptFunction($filePath);
putenv("$encryptedPath=$filePath");
?>
<!DOCTYPE html>
<html>
<body>
<img src = "<?php echo $encryptedPath;?>"/>
</body>
</html>
RewriteEngine on
RewriteRule (.+?)$ %{ENV:$1} [L,QSA]
Even there I'm not sure it can be very efficient and a looping script is needed on the server to dump the env variables regularly.
I have also looked about create a rewriting rules mapping file and edit it with php on the go but I don't really like this method.
So is it possible to call my php decryption function directly from the .htaccess file and redirect the client to the clear path? Or another method that I don't mentioned to do this?
I precise that I have the full control of the Apache server.
Thanks in advance for your answers :)