I want to access my static video files with expire time the video file will only work when it have valid token and expire time.
function generateSignedUrl($file, $secret, $expiry) {
$md5 = md5($file . $expiry . $secret);
return "https://example.com/files/$file?md5=$md5&expires=$expiry";
}
$file = "sample-file.mp4";
$secret = "secret-key";
$expiry = time() + 3600;
$signedUrl = generateSignedUrl($file, $secret, $expiry);
echo "Signed URL: $signedUrl";
i try to control it using .htaccess but it not working here is my htaccess code.
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)md5=([^&]+) [NC]
RewriteCond %{QUERY_STRING} (^|&)expires=([0-9]+) [NC]
RewriteCond %{TIME} >%3
RewriteRule ^files/(.*)$ - [F]
RewriteCond %{QUERY_STRING} (^|&)md5=([^&]+) [NC]
RewriteCond %{QUERY_STRING} (^|&)expires=([0-9]+) [NC]
RewriteCond %{REQUEST_URI} (.*) [NC]
RewriteCond %{ENV:SECURE_KEY} (secret-key) [NC]
RewriteCond %{QUERY_STRING} (^|&)md5=%{ENV:CALC_MD5} [NC]
RewriteRule ^files/(.*)$ /files/$1 [L]
RewriteCond %{QUERY_STRING} !(md5=.+&expires=.+) [NC]
RewriteRule ^files/ - [F]
i need that url will only work when it was access like this https://example.com/files/sample-file.mp4?md5=545f27b2a8b719c6ed87b1db624b1eb5&expires=1737717458
if someone try to access directly it with return 403 https://example.com/files/sample-file.mp4 this will not work.