我想知道如何才能解决下面的问题。在我的服务器的根目录上有一个名为upload.php的文件
我希望能够添加一个"/upload“(没有.php)到我的网址,浏览器应该要求输入密码(如果可能的话,还可以要求输入用户名)。我输入的密码(和用户名)是正确的,upload.php应该是打开的。
使用htaccess可以做到这一点吗?
发布于 2010-05-21 17:44:59
是的,这是两个截然不同的问题。
首先:删除.php扩展
主要有两种方法可以做到这一点。
Options +MultiViews启用它。RewriteRule ^upload$ upload.php。这也可以放在http.conf或.htaccess中的块中(如果激活)。您需要使用RewriteEngine on.启用mod_rewrite和重写引擎
秒:需要身份验证
使用PHP和Apache都可以做到这一点。
对于Apache,请参阅文档here.
function send401() {
$realm = "Credentials for upload";
header('WWW-Authenticate: Basic realm="'.$realm.'"');
header('HTTP/1.1 401 Unauthorized');
die();
}
function verify_credentials($user, $password) {
//check user and password here. Return true or false
return true;
}
if (!array_key_exists('PHP_AUTH_USER',$_SERVER) ||
!array_key_exists('PHP_AUTH_PW',$_SERVER)) {
send401();
}
elseif (!verify_credentials($_SERVER['PHP_AUTH_USER'],
$_SERVER['PHP_AUTH_PW']))
send401();
//if it gets here, the user was successfully authenticatedhttps://stackoverflow.com/questions/2880083
复制相似问题