要在 Apache 上隐藏某个目录,使其无法通过浏览器直接查看,但可以通过特定路径访问,以下是更详细的配置步骤:
2 N: w" K- A: z3 \" t$ _! u# w" p
1. 修改 Apache 的主配置文件
打开 Apache 的主配置文件或虚拟主机配置文件。假设你使用的是默认虚拟主机配置文件 /etc/apache2/sites-available/000-default.conf:
, t' b4 b3 G. C" a% Q
bash
复制代码
sudo nano /etc/apache2/sites-available/000-default.conf
2. 配置访问控制
为了隐藏 /mnt/mydisk/var/www/html/666目录,可以配置 Apache 不显示该目录的内容,除非通过特定路径访问。
* e2 A2 s' ~1 u% s6 C! C
在 000-default.conf 文件中添加以下配置:
" t1 h2 y( a$ S3 X
apache
复制代码
<VirtualHost *:80>
DocumentRoot /mnt/mydisk/var/www/html
% G0 F' D& V" s/ ?: X$ r& y1 ^! N
<Directory /mnt/mydisk/var/www/html>
Options -Indexes +FollowSymLinks
AllowOverride None
Require all granted
</Directory>
$ H* I* B5 C) E( P
Alias /secretpath /mnt/mydisk/var/www/html/666
<Directory /mnt/mydisk/var/www/html/666>
Options +Indexes
AllowOverride None
Require all granted
</Directory>
" n$ x# R0 V& u" ^
<Location /666>
Require all denied
</Location>
</VirtualHost>
3. 禁用 /666 目录的直接访问
通过 <Location> 指令来禁用直接访问 /666目录。
- ~8 H2 r$ u( o/ X/ M
4. 重启 Apache 服务
保存配置文件并退出编辑器,然后重启 Apache 服务:
: s- f3 j7 a+ ], g: m) M
bash
复制代码
sudo systemctl restart apache2
解释
DocumentRoot /mnt/mydisk/var/www/html:设置默认的文档根目录。
<Directory /mnt/mydisk/var/www/html>:配置默认文档根目录的访问权限,禁用目录索引。
Alias /secretpath /mnt/mydisk/var/www/html/666:创建一个别名,使得 /mnt/mydisk/var/www/html/666 目录可以通过 /secretpath 访问。
<Directory /mnt/mydisk/var/www/html/666>:配置 666目录的访问权限,允许目录索引。
<Location /666>:禁用直接通过 /work 路径访问 work 目录。
通过以上配置,访问者无法通过 http://yourdomain/work 访问 666 目录,但可以通过 http://yourdomain/secretpath 访问该目录的内容。
3 f$ G" W, ^7 e( j+ ~; ]; e
这样可以隐藏目录,同时保留特定路径的访问权限。