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

Apache子域名虚拟主机下多Magento 2站点子目录配置求助

解决子域名子目录下Magento 2站点的.htaccess重定向失效问题

问题根源在于你当前的.htaccess规则使用了绝对路径/pub/$0,而Magento 2站点部署在子目录(如/magentov1)下,这个绝对路径会将请求跳转到域名根目录的pub文件夹,而非当前子目录下的pub,导致重定向失效。

修复方案一:使用相对路径(通用版)

直接修改RewriteRule的目标路径,去掉开头的斜杠,让Apache基于当前.htaccess所在的子目录解析路径。每个子目录下的.htaccess都可以用这个通用配置:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/pub/
RewriteCond %{REQUEST_URI} !^/setup/
RewriteCond %{REQUEST_URI} !^/update/
RewriteCond %{REQUEST_URI} !^/dev/
RewriteRule ^(.*)$ pub/$1 [L]
DirectoryIndex index.php

修复方案二:硬编码子目录路径(精准匹配版)

如果需要更精准的路径匹配,可以将子目录名加入规则中,以magentov1为例:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/magentov1/pub/
RewriteCond %{REQUEST_URI} !^/magentov1/setup/
RewriteCond %{REQUEST_URI} !^/magentov1/update/
RewriteCond %{REQUEST_URI} !^/magentov1/dev/
RewriteRule .* /magentov1/pub/$0 [L]
DirectoryIndex index.php

注意:每个子目录(magentov2、magentov3)的.htaccess需要对应替换规则中的magentov1为自身目录名。

额外配置检查

确保Apache虚拟主机配置满足以下要求:

  • 开启mod_rewrite模块(可通过a2enmod rewrite命令启用)
  • 虚拟主机的目录配置中设置AllowOverride All,确保.htaccess规则能生效:
<Directory /var/www/mysubdomain>
    AllowOverride All
    Require all granted
</Directory>

修改完成后重启Apache服务(systemctl restart apache2),即可正常将子目录下的请求重定向到对应站点的pub文件夹。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 15:36:20