使用 .htaccess 仅授予对一个目录的访问权限

Mar*_*tyn 5 .htaccess

我只想提供对单个目录 (/dist) 的公共访问。我有一个 .htaccess 文件,其中包含以下内容:

Order deny,allow
Deny from all

<Directory /dist>
    Order Allow,Deny
    Allow from all
</Directory>
Run Code Online (Sandbox Code Playgroud)

我尝试首先拒绝所有内容的访问,然后使用 /dist 目录的允许规则覆盖该指令。但是,当我尝试通过浏览器访问任何文件时,我得到以下信息:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at you@example.com to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

sta*_*een 6

正如@Anubhava所说,htaccess上下文中不允许使用目录指令,该指令只能在目录上下文中使用。您可以使用 RewriteRule 拒绝访问除 /dist 目录之外的所有目录:

RewriteEngine on

RewriteRule !dist - [F]
Run Code Online (Sandbox Code Playgroud)

这将禁止除 /dist 请求之外的所有传入请求。