If I have a file located at http//site.com/files/foo.zip.

How can I rewrite this url to http://site.com/download/foo.zip, so the real URL doesn't show at all in the user's browser/download manager ?

Accepted Answer

I assume you have Apache and mean .htaccess.

RewriteEngine On
RewriteRule ^download/(.*)$ files/$1 [R,L]

Otherwise if you did want to use PHP, you would need to send those requests to a PHP script anyway with URL rewriting.


I want to restrict download access to registered users only.

This won't do that, your best bet is to move these files above the document root and serve them via PHP.

For example...

// Get into your system's context so we can determine if the user is logged in.
include 'everything.php';

if ( ! $loggedIn) {
   die('Log in mate!'); // Handle this somewhat better :)

$file = $_GET['file'];

// Get real path for $file.
$file = basename(__FILE__) . '/above/doc/root/files/' . $file;

if ( ! file_exists($file)) {
   die('This file does not exist!'); // And handle this better too.

header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Length: ' . filesize($file));
Written by alex
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki