> For the complete documentation index see https://domscan.net/llms.txt. Markdown versions of every docs page are available by appending .md to the page URL.

Source: https://domscan.net/zh/docs/redirect-chain

# 重定向链

通过 DomScan API，跟随 URL 重定向链以检测 HTTPS 升级、域名更改和最终登陆页面。 对于 SEO 分析、安全审计和理解 URL 路由很有用。

跟随 URL 重定向链以检测 HTTPS 升级、域名更改和最终登陆页面。对于 SEO 分析、安全审计和理解 URL 路由很有用。跟踪所有 3xx 重定向，包括 301、302、303、307 和 308。

GET /v1/redirects

#### 查询参数

| 参数 | 类型 | 说明 |
| --- | --- | --- |
| url 必需 | string | 要跟随的起始 URL（例如 "http://example.com" ） |
| max_redirects 可选 | number | 要跟随的最大重定向数（默认值：10，最大值：20） |
| domain 可选 | string | url 的别名，仅当您只有裸域名时使用。当两者都提供时，url 参数优先，且自动添加 https:// 协议。 |

#### 响应字段

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| `original_url` | string | 提供的起始 URL |
| `final_url` | string | 所有重定向后的最终 URL |
| `redirect_count` | number | 跟随的总重定向数 |
| `https_upgrade` | boolean | HTTP 是否升级到 HTTPS |
| `chain` | array | 每个重定向的 URL、状态代码、位置 |

#### 请求示例

```bash
curl -H "X-API-Key: your-api-key" "https://domscan.net/v1/redirects?url=http://github.com"
```

```javascript
const domscanFetch = (url, options = {}) =>
  fetch(url, {
    ...options,
    headers: { ...options.headers, "X-API-Key": "your-api-key" },
  });

const response = await domscanFetch(
  "https://domscan.net/v1/redirects?url=" + encodeURIComponent("http://github.com")
);
const data = await response.json();

console.log(`Final URL: ${data.final_url}`);
console.log(`Redirects: ${data.redirect_count}`);
console.log(`HTTPS upgrade: ${data.https_upgrade}`);

data.chain.forEach((hop, i) => {
  console.log(`${i + 1}. ${hop.status} ${hop.url} → ${hop.location}`);
});
```

```python
import requests

domscan = requests.Session()
domscan.headers.update({"X-API-Key": "your-api-key"})

from urllib.parse import quote

url = quote("http://github.com", safe='')
response = domscan.get(f"https://domscan.net/v1/redirects?url={url}")
data = response.json()

print(f"Final URL: {data['final_url']}")
print(f"HTTPS upgrade: {data['https_upgrade']}")
for i, hop in enumerate(data['chain']):
    print(f"{i+1}. [{hop['status']}] {hop['url']}")
```

#### 响应示例

```json
{
  "original_url": "http://github.com",
  "final_url": "https://github.com/",
  "redirect_count": 1,
  "chain": [
    {
      "url": "http://github.com",
      "status": 301,
      "location": "https://github.com/",
      "response_time_ms": 45
    }
  ],
  "https_upgrade": true,
  "domain_change": false,
  "total_time_ms": 156
}
```
