使用 Apache 列出目录内容
List directory contents with Apache
首先声明一下我对 Apache 的了解差不多 none,所以如果我使用的术语不正确,我深表歉意。
我有一个用Vue写的网站,路由由Vue Router负责。在他们的文档中,他们指定为了让路由器正常工作,您必须将其放入您网站的 .htaccess
文件中:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
据我所知,所有请求都被发送回 index.html
文件,该文件将负责根据路径加载正确的组件。
我的目标是现在让我的网站有一个路径(比方说 /documents
),它不会被 Vue 选择,而是显示目录的内容并允许您导航和下载内容(如this)。
我尝试了几种方法,但它们都是 return 403 或 500(可能是由于我的配置错误)。我知道我需要添加一个 RewriteRule
但我尝试的所有那些 return 奇怪的错误。
提前致谢
您可以根据 RewriteBase
的内容设置多个重写规则。在您当前的设置中,该规则适用于主机的根目录。
您可以使用 RewriteBase /documents/
添加另一条规则。更多信息:What does RewriteBase do and how to use it?
我建议阅读文档:https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
The RewriteCond directive defines a rule condition.
所以这里有一个肮脏的解释:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
所以你的 RewriteConds 说如果给定的 path/url 不是文件 (!-f) 而不是目录 (!-d) 那么下一个重写规则 (RewriteRule . /index.html [L]) 采取行动。
RewriteRule . /index.html [L]
“。”是通配符,因此所有 url 都将重定向到 index.html。
[L] 标志停止执行 (https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_l)
如果 url 是 index.html,RewriteRule ^index\.html$ - [L]
停止执行。
因此,您的重写规则符合您的要求并且看起来是正确的。
当您收到 403 时,您可能需要将 Options +Indexes
添加到您的配置或 htaccess。
最后看了文档,还是没搞明白怎么设置。我找到了 this page,并且使用选项 #2 我能够让目录至少显示出来。
然后我通过 .htaccess
文件将身份验证添加到文件夹,并使用 username/password 组合
添加 .htpasswd
文件
TLDR
- 在您想要的位置创建文件夹。在我的例子中是
httpdocs/documents
- 创建一个
.htaccess
文件,在其中放置以下内容:
# Omit this section if you do not need the auth
AuthType Basic
AuthName "restricted area"
AuthUserFile /path/to/your/.htpasswd
require valid-user
Order allow,deny
Allow from all
Options +Indexes
- 在上面指定的位置创建
.htpasswd
文件。为了生成 username/password 组合,我使用了 this
欢迎大家指正!
首先声明一下我对 Apache 的了解差不多 none,所以如果我使用的术语不正确,我深表歉意。
我有一个用Vue写的网站,路由由Vue Router负责。在他们的文档中,他们指定为了让路由器正常工作,您必须将其放入您网站的 .htaccess
文件中:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
据我所知,所有请求都被发送回 index.html
文件,该文件将负责根据路径加载正确的组件。
我的目标是现在让我的网站有一个路径(比方说 /documents
),它不会被 Vue 选择,而是显示目录的内容并允许您导航和下载内容(如this)。
我尝试了几种方法,但它们都是 return 403 或 500(可能是由于我的配置错误)。我知道我需要添加一个 RewriteRule
但我尝试的所有那些 return 奇怪的错误。
提前致谢
您可以根据 RewriteBase
的内容设置多个重写规则。在您当前的设置中,该规则适用于主机的根目录。
您可以使用 RewriteBase /documents/
添加另一条规则。更多信息:What does RewriteBase do and how to use it?
我建议阅读文档:https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
The RewriteCond directive defines a rule condition.
所以这里有一个肮脏的解释:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
所以你的 RewriteConds 说如果给定的 path/url 不是文件 (!-f) 而不是目录 (!-d) 那么下一个重写规则 (RewriteRule . /index.html [L]) 采取行动。
RewriteRule . /index.html [L]
“。”是通配符,因此所有 url 都将重定向到 index.html。 [L] 标志停止执行 (https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_l)
如果 url 是 index.html,RewriteRule ^index\.html$ - [L]
停止执行。
因此,您的重写规则符合您的要求并且看起来是正确的。
当您收到 403 时,您可能需要将 Options +Indexes
添加到您的配置或 htaccess。
最后看了文档,还是没搞明白怎么设置。我找到了 this page,并且使用选项 #2 我能够让目录至少显示出来。
然后我通过 .htaccess
文件将身份验证添加到文件夹,并使用 username/password 组合
.htpasswd
文件
TLDR
- 在您想要的位置创建文件夹。在我的例子中是
httpdocs/documents
- 创建一个
.htaccess
文件,在其中放置以下内容:
# Omit this section if you do not need the auth
AuthType Basic
AuthName "restricted area"
AuthUserFile /path/to/your/.htpasswd
require valid-user
Order allow,deny
Allow from all
Options +Indexes
- 在上面指定的位置创建
.htpasswd
文件。为了生成 username/password 组合,我使用了 this
欢迎大家指正!