我可以使用URL重写来隐藏真实文件URL吗?

Ale*_*lex 2 php url-rewriting

如果我的文件位于http // site.com/files/foo.zip.

如何将此URL重写为http://site.com/download/foo.zip,以便在用户的浏览器/下载管理器中根本不显示真实的URL?

ale*_*lex 6

我假设你有Apache和意思.htaccess.

RewriteEngine On
RewriteRule ^download/(.*)$ files/$1 [R,L]
Run Code Online (Sandbox Code Playgroud)

否则,如果您确实想要使用PHP,则无论如何都需要通过URL重写将这些请求发送到PHP脚本.

更新

我想限制只对注册用户的下载访问.

这不会那样做,最好的办法是将这些文件移到文档根目录上并通过PHP提供.

例如...

<?php
// 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));
readfile($file);
exit;
Run Code Online (Sandbox Code Playgroud)