htaccess 密码保护和 mod 重写?

发布于 2024-09-03 03:05:54 字数 186 浏览 3 评论 0原文

我想知道如何解决以下问题。 在我的服务器的根目录中有一个名为 upload.php 的文件,

我希望能够在我的 URL 中添加“/upload”(不带 .php),并且浏览器应该要求输入密码(如果可能的话,可能还需要用户名)。我正确输入密码(和用户名),upload.php 应该打开。

htaccess 可以做到这一点吗?

i wonder how i can solve the following problem.
on the root directory of my server lies a file calles upload.php

i want to be able to add a "/upload" (without .php) to my URL and the browser should ask for a password (and maybe username if possible). I i enter the password (and username) correctly upload.php should be opened.

Is this possible with htaccess?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦中的蝴蝶 2024-09-10 03:05:54

是的,这是两个不同的问题。

第一:删除.php扩展名

主要有两种方法。

  • 启用内容协商抛出多视图。这将允许您引用不带扩展名的其他资源,甚至可以拥有多个名称相似但扩展名不同的文件,并让 Apache 根据浏览器的偏好选择最佳的文件。请参阅链接。您可以使用中的 Options +MultiViews 启用它。如果您允许覆盖此选项,请在 http.conf 或 .htaccess 中阻止。
  • 使用mod_rewrite。专门针对您的情况的规则可能是RewriteRule ^upload$ upload.php。这也可以放在中。 http.conf 或 .htaccess 中的块(如果已激活)。您需要启用 mod_rewrite 并使用 RewriteEngine on 启用重写引擎。

秒:需要身份验证

您可以使用 PHP 和 Apache 来完成此操作。

  • 对于 Apache,请参阅此处的文档。
  • 对于 PHP,使用基本身份验证(请注意,密码将以纯文本形式发送到服务器,除非您使用 https,否则可能会被监视您流量的人窥探),您可以执行以下操作

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 authenticated

Yes, both those are two distinct questions.

First: remove the .php extension

There are mainly two ways of doing this.

  • Enable content negotiation throws MultiViews. This will allow you to refer to other resources without extension and even have several files with similar names but different extensions and let Apache pick the best according to what the browser prefers. See the link. You can enable it with Options +MultiViews in a <Directory> block in http.conf or .htaccess if you allows override for this option.
  • Use mod_rewrite. A rule specifically for your case could be RewriteRule ^upload$ upload.php. This can also be put in a <Directory> block in http.conf or .htaccess (if activated). You will need to have enabled mod_rewrite and the rewrite engine with RewriteEngine on.

Seconds: require authentication

You can do this both with PHP and Apache.

  • For Apache, see the docs here.
  • For PHP, using basic authentication (be warned the password will be sent to the server in plain text and unless you are using https it may be snooped by someone watching your traffic), you can do something like this:

 

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 authenticated
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文