我在 Docker 容器中的 Apache2 的 Web 主页中使用了以下 .htaccess 文件:
# allows HTML files to be interpretted as PHP files
AddType application/x-httpd-php .html
# allows redirect to *.html files without using the extension
# e.g., http://example.com/foo will show http://example.com/foo.html
<IfModule mod_rewrite.c>
Options All
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
#RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]
RewriteRule !.*\.html$ %{REQUEST_URI}.html [L]
# causes redirect from HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>
Options -Indexes
# environament variables
SetEnv DBL "mysql:dbname=roars;host=foo;port=3306"
SetEnv DB_USER "bar"
SetEnv DB_PASS "glorp"
Options -Indexes
指令不会阻止我们查看目录。所有其他指令都正常工作,当我在 Docker 容器外部使用此 .htaccess 文件时,它通过禁止查看目录来正常工作。
我是不是做错了什么,或者这是 Docker 容器特有的?
最佳答案
我在 Docker 容器中的 Apache2 默认配置文件中发现了问题:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName dev.f0o.com
DocumentRoot /home/foo/src/www-server/
#ErrorLog ${APACHE_LOG_DIR}/error.log
#CustomLog ${APACHE_LOG_DIR}/access.log combined
ErrorLog /var/log/apache2/error.log
CustomLog /var/log/apache2/access.log combined
</VirtualHost>
<Directory /home/foo/src/www-server/>
# allow .htaccess overrides to work
AllowOverride All
DirectoryIndex login.html index.html index.php
</Directory>
# this matches a link to any device directory to the physical webui directory
#AliasMatch ^/projects/([-\w]*)/css/(.*)$ /home/foo/src/www-server/static/css/$2
#AliasMatch ^/projects/([-\w]*)/fonts/(.*)$ /home/foo/src/www-server/static/fonts/$2
#AliasMatch ^/projects/([-\w]*)/images/(.*)$ /home/foo/src/www-server/static/images/$2
#AliasMatch ^/projects/([-\w]*)/lib/(.*)$ /home/foo/src/www-server/static/lib/$2
#AliasMatch ^/projects/([-\w]*)/scripts/(.*)$ /home/foo/src/www-server/static/scripts/$2
AliasMatch ^/projects/([-\w]*)/(.*)$ /home/foo/src/www-server/client/$2
<Directory /home/foo/src/www-server/client>
DirectoryIndex home.html
Options All
AllowOverride All
Require all granted
</Directory>
解决方案 1 - 该问题首先在 /home/foo/src/www-server/client
中被发现,它在默认配置文件。允许目录列表,因为 Options
设置为 All
。将其更改为 Options -Indexes
解决了这个问题。
解决方案 2 - 作为实验,我注释掉了以 Options
开头的行,结果是我想要的,可能是因为 .htaccess 文件被允许执行它的与默认配置文件没有冲突的作业。
因为我通常不是配置 Web 服务器的人,所以我相信鉴于我们现在已知的集合,可能会有更好的方法来解决这个问题。那些在 Apache Web 服务器配置方面拥有更多专业知识的人可能会有更优雅的解决方案。
关于apache - 选项 - 索引在 Docker 容器中的 .htaccess 文件中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36899147/