Search code examples
php.htaccessurl-masking

Masking filename looking URL with PHP


since this is the first time someone made me click some referral link i got very curious. Here in the screenshot you will see a masked url which directly goes to some referral link in some other site. Masked Referral Link

The interesting part which got my attention and the reason i clicked the link is that link has a file extension as you will notice. I can think of PHP redirects, .htaccess redirects but couldn't exactly think a solution for filenames masking.

As usually we can parse the url in .htaccess and redirect .zip extensions to a php file to check whether the file is valid filename or a key value for redirect in database (This is a solution too) but i didn't notice any redirect on this link.

Is there any other possible ways for this?


Solution

  • You can redirect any request with .htaccess. For example:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,L]
    

    will redirect all requests to index.php, in which you can check if the user wants file.zip just by looking into $SERVER['REQUEST_URI']. Then serve him the file you want:

    $zip = '/path/to/zip/file/outside/the/web/root.zip';
    
    // set correct headers, so the browser identifies the response as a zip file
    header('Content-type: application/zip;');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: ' . filesize($zip) . ';');
    header('Content-Disposition: attachment; filename="whatever.zip";');
    
    // output the file contents
    readfile($zip);