大网数据广告活动
网站运营与故障问答

Password protect specific files on root of website

· 52CE 我爱测

I have had a website running for a while and it has a small following. What I wish to do now is password protect some specific files that sit in the root directory. So I'd like the user not to see any username/password request un…

问题原文

原帖链接 · Mick · CC BY-SA 4.0

I have had a website running for a while and it has a small following. What I wish to do now is password protect some specific files that sit in the root directory. So I'd like the user not to see any username/password request unless they attempt to access one of these files.

On top of that I also want the user to see a specific message as they are presented with the login request - something like:

Sorry about this, please contact [email omitted] for a username and password.

However, I've read that the htaccess/AuthName text does not get displayed on the login popup on Chrome so I need an alternative.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

I read that the htaccess/AuthName text does not get displayed on the login popup on chrome so I need an alternative.

The "alternative" is to role your own authentication system (which could still use HTTP Authentication together with your own server-side scripts) - which is beyond the scope of this answer (and Webmasters SE). (HINT: You would need to implement a front-controller pattern and rewrite these requests to your server-side script that then handles the authentication and response back to the client.)

A workaround is to set a custom "401 Unauthorised" response (either a simple text string or complete HTML page) with your desired message. But this is only displayed when authentication fails, ie. the user enters an invalid username/password combo and hits "OK" or the password dialog is cancelled.

NB: You still need to set the AuthName directive as this also determines which username/password combo the browser sends as part of the request (after successful authentication).

For example:

# Simple text string
ErrorDocument 401 "Sorry about this, please contact [email omitted] for a username and password"

OR

# Complete HTML error document containing custom response
ErrorDocument 401 /errordocs/401.html

Then, how you protect the "specific files" really depends on the filenames and perhaps how many.

For example, if there is a pattern to the filenames, eg. They all start with the word "example" then you could wrap the authentication directives in a <Files> wrapper:

<Files "example*.*">
    # Authentication directives go here...
    AuthType Basic
    AuthName "Protected File"
    AuthUserFile "/absolute/file/path/to/password-file"
    Require valid-user
</Files>

The Files directive uses simple wildcard matching. Use FilesMatch to match against a regex. For example, to match a hand full of unrelated .html files then you could do something like:

<FilesMatch "^(one|two|three|four|five)\.html$">
    # Authentication directives go here (see above)
    :
</FilesMatch>

If you have many files then perhaps consider setting an environment variable when one of these files is requested and use this env var in the authentication directives. For example (Apache 2.4):

# Set the AUTH_REQUIRED env var if a protected file is requested
SetEnvIf Request_URI "^/one\.html$" AUTH_REQUIRED
SetEnvIf Request_URI "^/two\.html$" AUTH_REQUIRED
SetEnvIf Request_URI "^/three\.html$" AUTH_REQUIRED
SetEnvIf Request_URI "^/four\.html$" AUTH_REQUIRED
SetEnvIf Request_URI "^/five\.html$" AUTH_REQUIRED

# Authentication...
<RequireAny>
    <RequireAll>
        # Unrestricted access to non-protected files
        Require all granted
        Require not env AUTH_REQUIRED
    </RequireAll>
    <RequireAll>
        # Authentication directives go here (see above)
        :
    </RequireAll>
</RequireAny>

Alternatively, use an Apache <If> expression:

# Authentication...
<If "reqenv('AUTH_REQUIRED') == '1'">
    # Authentication directives go here (see above)
    :
</If>

Although, strictly speaking, the above (SetEnvIf directive) is checking the requested URL-path, not the underlying file-path.

This last example is also dependent on other directives you might have in your .htaccess file. For example, if you are doing any URL rewriting then then the above env var may be renamed to REDIRECT_AUTH_REQUIRED. And you will need to check for this in the authentication directives instead.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2022-08-11)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。