You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache2如何将站点请求重定向到public目录并隐藏路径?

Apache 子目录站点隐藏public目录配置方案

问题表现

访问localhost/qwerty时显示目录结构列表,需点击public文件夹才能进入主页,地址栏会出现/public段,未达到隐藏public目录、禁止访问核心代码目录的预期。
问题截图

现有环境信息

  • 站点部署路径:/var/www/html/qwerty/public
  • 目录结构:
|--/var/www/html
|  |--index.html
|  |--qwerty
|  |  |--.htaccess
|  |  |--public
|  |  |  |--index.php
  • 现有.htaccess配置:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
  • 现有虚拟主机配置/etc/apache2/sites-available/qwerty.conf:
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/qwerty/public
    ServerName qwerty

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
  • 已执行命令:
sudo a2ensite qwerty
sudo systemctl restart apache2

根因说明

  • 配置的虚拟主机绑定ServerName为qwerty,仅访问http://qwerty时才会匹配该规则,访问localhost/qwerty走的是Apache默认的/var/www/html根站点,未触发虚拟主机配置。
  • 默认站点开启了目录索引功能(mod_autoindex),访问无默认首页的目录时直接展示文件列表。
  • 现有重写规则未判断请求资源是否真实存在,也未配置非public目录的访问限制。
  • 站点目录未开启.htaccess配置覆盖权限,写的重写规则未生效。

配置方案

根据访问需求二选一即可。

方案1:保留localhost/qwerty子路径访问(无需修改hosts)

  • 替换/var/www/html/qwerty/.htaccess内容为如下配置:
<IfModule mod_rewrite.c>
    RewriteEngine On
    # 现有文件、目录直接访问,不做重写
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # 其余请求统一转发到public目录
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

# 全局禁止访问qwerty目录下的内容
<IfModule mod_authz_core.c>
    Require all denied
</IfModule>

# 单独放开public目录访问权限
<Directory "/var/www/html/qwerty/public">
    <IfModule mod_authz_core.c>
        Require all granted
    </IfModule>
    # 配置默认首页优先级
    DirectoryIndex index.php index.html
    # 关闭目录列表展示
    Options -Indexes
</Directory>
  • 编辑Apache默认站点配置文件/etc/apache2/sites-available/000-default.conf,在<VirtualHost *:80>块内添加如下配置,开启.htaccess覆盖权限:
<Directory "/var/www/html/qwerty">
    AllowOverride All
    Options -Indexes
    Require all granted
</Directory>
  • 禁用之前配置的无效虚拟主机,避免配置冲突:
sudo a2dissite qwerty
  • 开启Apache重写模块,重启服务生效:
sudo a2enmod rewrite
sudo systemctl restart apache2

方案2:独立域名http://qwerty访问(生产环境推荐)

  • 编辑本地hosts文件,将qwerty域名指向本地:
    • Linux/macOS路径:/etc/hosts
    • Windows路径:C:\Windows\System32\drivers\etc\hosts
      在文件末尾添加一行:
    127.0.0.1 qwerty
    
  • 替换/etc/apache2/sites-available/qwerty.conf内容为如下配置:
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/qwerty/public
    ServerName qwerty

    # public目录权限配置
    <Directory "/var/www/html/qwerty/public">
        AllowOverride All
        Options -Indexes
        Require all granted
        DirectoryIndex index.php index.html
        <IfModule mod_rewrite.c>
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.*)$ index.php/$1 [L]
        </IfModule>
    </Directory>

    # 禁止访问非public的核心代码目录
    <Directory "/var/www/html/qwerty">
        Require all denied
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/qwerty_error.log
    CustomLog ${APACHE_LOG_DIR}/qwerty_access.log combined
</VirtualHost>
  • 开启重写模块、启用站点、重启服务生效:
sudo a2enmod rewrite
sudo a2ensite qwerty
sudo systemctl restart apache2

配置完成后清空浏览器缓存再测试,即可实现:访问对应地址直接加载主页,地址栏无public段,静态资源按路径自动映射,核心代码目录无法被外部直接访问。

内容的提问来源于stack exchange,提问作者user11553898

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 16:51:26