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

使用wget镜像网站返回404错误,浏览器可正常访问的问题排查与解决

问题

尝试使用wget镜像网站时收到404错误,但该网站可通过浏览器正常访问。

使用的命令

wget --mirror --convert-links --adjust-extension --page-requisites --no-parent --execute robots=off --user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36" -P D:\client-sites\maharjanmetal.com.np https://maharjanmetal.com.np/products

错误输出

--2024-10-23 21:12:43-- https://maharjanmetal.com.np/products
Resolving maharjanmetal.com.np (maharjanmetal.com.np)... 149.100.146.116
Connecting to maharjanmetal.com.np (maharjanmetal.com.np)|149.100.146.116|:443... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://maharjanmetal.com.np/products/ [following]
--2024-10-23 21:12:43-- https://maharjanmetal.com.np/products/
Reusing existing connection to maharjanmetal.com.np:443.
HTTP request sent, awaiting response... 404 Not Found
2024-10-23 21:12:44 ERROR 404: Not Found.

环境与现象

  • 环境:Windows 11
  • 预期行为:wget应下载网站内容及资源,创建本地镜像
  • 实际行为:浏览器可正常访问网站,但wget跟随301重定向从/products到/products/后触发404,未下载任何文件

疑问

为何浏览器可正常访问该URL,但wget跟随301重定向后持续出现404错误?如何通过wget成功镜像该网站?


解答

原因分析

  1. 服务器路由规则差异:不少现代网站(尤其是基于前端框架构建的单页应用)的服务器对URL末尾斜杠的处理逻辑和浏览器前端路由不同。浏览器访问时,前端路由会自动兼容有无斜杠的URL;但wget作为纯HTTP客户端,严格遵循服务器返回的重定向指令,而服务器可能未对带斜杠的/products/配置有效路由,导致返回404。
  2. 请求头不完整:虽然已设置User-Agent,但浏览器会自动发送更多请求头(如Accept、Accept-Language等),服务器可能通过这些头信息判断请求来源,若缺失则返回错误。

解决方法

方法1:禁用重定向跟随,直接请求原始URL

添加--max-redirect=0参数,阻止wget跟随301重定向,直接获取无斜杠URL的内容:

wget --mirror --convert-links --adjust-extension --page-requisites --no-parent --execute robots=off --user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36" --max-redirect=0 -P D:\client-sites\maharjanmetal.com.np https://maharjanmetal.com.np/products

方法2:补充完整请求头,模拟真实浏览器请求

添加浏览器常用的请求头,让服务器识别为合法浏览器请求:

wget --mirror --convert-links --adjust-extension --page-requisites --no-parent --execute robots=off --user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36" --header="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8" --header="Accept-Language: en-US,en;q=0.5" --header="Connection: keep-alive" --header="Upgrade-Insecure-Requests: 1" -P D:\client-sites\maharjanmetal.com.np https://maharjanmetal.com.np/products

方法3:镜像网站根目录再筛选内容

若子页面路由存在问题,可先镜像整个网站根目录,之后再提取需要的/products相关内容:

wget --mirror --convert-links --adjust-extension --page-requisites --no-parent --execute robots=off --user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36" -P D:\client-sites\maharjanmetal.com.np https://maharjanmetal.com.np/

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 02:32:03