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

如何用.htaccess将含controller=new-products的404 URL重定向至首页?

解决包含复杂查询字符串的404页面重定向问题

问题背景

需要将所有包含controller=new-products的URL(无论后续查询字符串多复杂)重定向至首页,目标URL示例:

index.php?controller=new-products&%253B%253B%25253Bamp%25253B%25253B%25253B%2525253Bamp%2525253B%2525253B%2525253B%252525

之前规则失效原因

  • Test 1:RewriteRule ^/?$仅匹配空根路径,无法匹配实际请求的/index.php
  • Test 2:查询字符串正则^controller=new-products&([0-9a-z_-]+)$限制过严,无法匹配包含URL转义字符(如%25)的复杂参数
  • Test 3:仅匹配带两位语言前缀的路径(如/en/index.php),若你的URL无此前缀则不生效

可行的.htaccess规则

情况1:URL无语言前缀(直接是/index.php开头)

将以下规则放在.htaccess文件最顶部(确保优先匹配):

RewriteEngine On

# 重定向所有含controller=new-products的index.php请求到首页
RewriteCond %{QUERY_STRING} (^|&)controller=new-products(&|$) [NC]
RewriteRule ^index\.php$ /? [L,R=301]

情况2:URL带两位语言前缀(如/en/index.php)

若你的站点有语言前缀路径,使用此规则:

RewriteEngine On

# 重定向带语言前缀且含目标参数的请求到首页
RewriteCond %{QUERY_STRING} (^|&)controller=new-products(&|$) [NC]
RewriteRule ^[a-z]{2}/index\.php$ /? [L,R=301,NC]

规则说明

  • (^|&)controller=new-products(&|$):匹配查询字符串中任意位置的controller=new-products,不管参数在开头、中间还是结尾
  • [NC]:不区分大小写,兼容大小写不一致的参数
  • /?:重定向到首页并清空查询字符串,避免携带原复杂参数
  • R=301:永久重定向,利于搜索引擎收录更新
  • L:停止执行后续规则,确保此重定向优先生效

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 13:15:20