Apache带子目录的重定向配置问题求助
解决Apache重定向规则不生效的问题
首先咱们先对齐你的核心需求:
- 访问
product.com(包括所有子路径,除了/something)时,重定向到目标网站A - 访问
product.com/something时,重定向到目标网站B
当前配置的问题分析
- 重定向规则顺序搞反了:在
www.product.com:443的配置里,RedirectPermanent /是全局匹配规则,会直接覆盖掉前面的Redirect "/something"——因为Apache处理规则是从上到下,全局规则会捕获所有请求,包括/something,导致特殊路径的重定向根本没机会生效。 - 域名跳转逻辑不符合需求:你现在把
product.com(不带www)的请求全跳转到www.product.com,但你的需求是直接对product.com的请求做定向,这完全违背了你的目标,自然子目录也不会按预期跳转。 - HTTP协议的规则缺失:当前80端口的
product.com配置只有全局跳转,没处理/something的特殊规则,导致HTTP访问这个路径时也不会生效。
修正后的完整配置
# 处理 HTTPS 的 product.com 请求 <VirtualHost *:443> ServerName product.com # 特殊路径优先处理,必须放在全局规则前面 RedirectPermanent "/something" "https://www.anotherredirectwebsite.com/something-name-product" # 所有其他路径统一重定向到目标网站A RedirectPermanent / "https://www.redirectsite.com/" Include ssl-conf/product_ssl.conf </VirtualHost> # 处理 HTTPS 的 www.product.com 请求(如果需要保留这个域名的跳转,可按需调整) <VirtualHost *:443> ServerName www.product.com RedirectPermanent "/something" "https://www.anotherredirectwebsite.com/something-name-product" RedirectPermanent / "https://www.redirectsite.com/" Include ssl-conf/product_ssl.conf </VirtualHost> # 处理 HTTP 的 product.com 请求 <VirtualHost *:80> ServerName product.com RedirectPermanent "/something" "https://www.anotherredirectwebsite.com/something-name-product" RedirectPermanent / "https://www.redirectsite.com/" </VirtualHost> # 处理 HTTP 的 www.product.com 请求 <VirtualHost *:80> ServerName www.product.com RedirectPermanent "/something" "https://www.anotherredirectwebsite.com/something-name-product" RedirectPermanent / "https://www.redirectsite.com/" </VirtualHost>
关键调整说明
- 规则顺序优先:把更具体的
/something规则放在全局规则前面,让Apache先匹配特殊路径,再处理其他所有请求。 - 修正域名跳转逻辑:移除了
product.com转www.product.com的规则,直接在product.com的VirtualHost里配置你需要的重定向目标。 - 覆盖所有场景:确保HTTP和HTTPS的
product.com、www.product.com都配置了对应规则,避免遗漏任何访问方式。
验证方法
修改配置后记得重启Apache服务:
sudo systemctl restart apache2
可以用curl测试重定向是否生效:
# 测试 /something 路径 curl -I https://product.com/something # 测试其他子路径 curl -I https://product.com/any-subpath
内容的提问来源于stack exchange,提问作者Jordi
相关产品推荐
相关产品推荐

