《JS跳转代码》根据爬虫、设备、来源判断,实现js屏蔽/跳转与展示控制
JS 多场景跳转模板,内置完整蜘蛛 UA 库,支持三种常用访问控制,配置区域配置对应模块即可,自带防循环重定向,可跳转域名或目录。《爬虫/蜘蛛/设备标识全量识别清单》
http://www.sheepyc.com/thread-703-1-1.html
本文中的爬虫/蜘蛛/设备标识更新/补充,均会在上述文章内同步,复制代码后注意核对
<script type="text/javascript">
// ==============================================
// JS 跳转控制通用模板
// 功能:爬虫识别 | 设备识别 | 来源识别 | 防循环重定向
// 支持:域名前缀跳转 / 完整域名跳转 / 指定目录跳转
// ==============================================
// ====================== 【配置区】 按需修改即可 ======================
// 1. 通用配置
const redirect_mode = 'replace'; // 跳转方式:replace=无历史记录跳转href=保留历史记录跳转
//replace:覆盖当前历史,返回按钮回不到跳转前页面
//href:新增历史,返回可以回到跳转前页面
// 2. 爬虫配置
const spider_redirect = ''; // 爬虫跳转目标,空=放行不跳转;示例:/spider.html 或 https://xxx.com
// 3. 设备跳转配置(留空=该设备不执行跳转)
const pc_redirect_target = ''; // PC端跳转目标;示例:www1.${base_domain} 或 /pc/
const tablet_redirect_target = ''; // 平板端(iPad等)跳转目标;示例:www1.${base_domain} 或 /pad/
const mobile_redirect_target = ''; // 手机端跳转目标;示例:m.${base_domain} 或 /mobile/
// 4. 来源跳转配置
const non_search_redirect = ''; // 非搜索引擎&非AI来源跳转目标,空=不启用;示例:/error.html
const ai_referer_redirect = ''; // AI平台来源单独跳转目标,空=不单独处理
// 5. 防循环重定向规则(匹配该规则则取消跳转,避免死循环)
// 域名前缀跳转填:^www1\. ;目录跳转填:^/mobile/
const loop_guard_pattern = '';
// ====================== 【配置区结束】 ======================
// ---------------------- 1. 基础域名提取 ----------------------
// 自动移除 www. / www1. / m. / wap. 前缀,提取裸域名
let base_domain = location.host;
const hostMatch = location.host.match(/^(www|www1|m|wap)\.(.+)$/i);
if (hostMatch) {
base_domain = hostMatch;
}
// ---------------------- 2. 设备类型精准识别 ----------------------
// 初始化标记
let is_pc = false;
let is_tablet = false;
let is_mobile = false;
const ua = navigator.userAgent || '';
// 优先级1:识别手机端
if (/mobile|iphone|ipod|android.*mobile|windows\s*phone|blackberry|opera\s*mini|ucweb|meego|silk|kindle|playbook|nintendo|playstation|wii/i.test(ua)) {
is_mobile = true;
}
// 优先级2:识别平板端(iPad/安卓平板,避免误判为PC)
if (!is_mobile) {
if (/ipad|android.*tablet|tablet/i.test(ua)) {
is_tablet = true;
}
}
// 优先级3:剩余识别为PC桌面端
if (!is_mobile && !is_tablet) {
if (/windows\s*nt|macintosh|mac\s*os\s*x|x11|cros|linux\s*(x86|amd64|i686)/i.test(ua)) {
is_pc = true;
}
}
// ---------------------- 3. 爬虫/蜘蛛全量识别 ----------------------
// 覆盖范围:通用泛匹配 + 全球主流AI蜘蛛 + 中国主流AI蜘蛛 + 全球搜索引擎 + 中国搜索引擎 + 其他平台蜘蛛
let is_spider = false;
const spiderRegex = /bot|spider|crawl|crawler|index|archive|gptbot|oai-searchbot|chatgpt-user|claudebot|claude-searchbot|claude-user|xai-searchbot|xai-bot|xai-grok|meta-externalagent|meta-externalfetcher|perplexitybot|perplexity-user|huggingfacebot|amazonbot|coherebot|inflectionbot|google-extended|ccbot|doubaobot|yuanbaobot|hunyuanbot|qwenbot|tongyibot|wenxinbot|moonshotbot|deepseekbot|kimibot|zhipubot|youbot|google|bing|yahoo|yandex|duckduckbot|slurp|applebot|baidu|sogou|bytespider|shenma|youdao|360spider|soso|toutiao|semrush|ahrefs|petalbot|exabot|facebot|facebook|twitter|linkedin|telegram|ia_archiver/i;
if (spiderRegex.test(ua)) {
is_spider = true;
}
// ---------------------- 4. 访问来源(Referrer)识别 ----------------------
let is_search_referer = false;
let is_ai_referer = false;
const referrer = document.referrer || '';
// 识别搜索引擎来源(支持多级域名,如google.co.jp、baidu.com.cn)
const searchReg = /\.(google|bing|yahoo|yandex|duckduckgo|apple|baidu|sogou|shenma|sm|youdao|360|soso|toutiao)(\.+){0,2}\//i;
if (searchReg.test(referrer)) {
is_search_referer = true;
}
// 识别AI平台来源
const aiReg = /\.(openai|anthropic|claude|xai|perplexity|huggingface|cohere|inflection|meta|gemini|doubao|wenxin|tongyi|qianwen|hunyuan|moonshot|deepseek)(\.+){0,2}\//i;
if (aiReg.test(referrer)) {
is_ai_referer = true;
}
// ---------------------- 5. 跳转目标计算(优先级从高到低) ----------------------
let final_target = '';
// 优先级1:爬虫专属逻辑
if (is_spider) {
final_target = spider_redirect;
}
// 优先级2:AI平台来源单独跳转(非爬虫时生效)
if (!is_spider && is_ai_referer) {
final_target = ai_referer_redirect;
}
// 优先级3:非搜索&非AI来源跳转(非爬虫、未触发AI跳转时生效)
if (!is_spider && !is_search_referer && !is_ai_referer) {
final_target = non_search_redirect;
}
// 优先级4:设备维度跳转(非爬虫、未触发来源跳转时生效)
if (!is_spider && final_target === '') {
if (is_pc) {
final_target = pc_redirect_target;
}
if (is_tablet) {
final_target = tablet_redirect_target;
}
if (is_mobile) {
final_target = mobile_redirect_target;
}
}
// 处理域名前缀模板变量,替换 ${base_domain} 为实际裸域名
if (final_target && final_target.includes('${base_domain}')) {
final_target = final_target.replace('${base_domain}', base_domain);
}
// ---------------------- 6. 防循环重定向检测 ----------------------
if (loop_guard_pattern !== '') {
const loopReg = new RegExp(loop_guard_pattern, 'i');
// 域名前缀场景防护
if (loopReg.test(location.host)) {
final_target = '';
}
// 目录跳转场景防护
if (loopReg.test(location.pathname)) {
final_target = '';
}
}
// ---------------------- 7. 执行跳转 ----------------------
if (final_target !== '') {
// 目标包含http/https → 直接跳转到完整域名
if (/^https?:\/\//i.test(final_target)) {
redirect_mode === 'replace'
? window.location.replace(final_target)
: window.location.href = final_target;
}
// 目标以/开头 → 站内目录跳转
else if (final_target.startsWith('/')) {
redirect_mode === 'replace'
? window.location.replace(final_target)
: window.location.href = final_target;
}
// 其他情况 → 视为域名前缀,拼接完整域名+原请求路径
else {
const fullUrl = 'http://' + final_target + location.pathname + location.search;
redirect_mode === 'replace'
? window.location.replace(fullUrl)
: window.location.href = fullUrl;
}
}
</script>
下一页是独立功能版本
独立功能版本:
版本1:js网站跳转代码(userAgent爬虫判断,蜘蛛正常,用户任何形式进入网站都跳转)(直接粘贴到页面即可,建议放<head>后面直接加载):
<script type="text/javascript">
// 爬虫UA特征正则 - 匹配优先级按分类从高到低排列
// 分类顺序:通用泛匹配 > 全球主流AI蜘蛛 > 中国主流AI蜘蛛 > 全球主流搜索引擎 > 中国主流搜索引擎 > 其他类爬虫
const spiderRegex = /bot|spider|crawl|crawler|index|archive|gptbot|oai-searchbot|chatgpt-user|claudebot|claude-searchbot|claude-user|xai-searchbot|xai-bot|xai-grok|meta-externalagent|meta-externalfetcher|perplexitybot|perplexity-user|huggingfacebot|amazonbot|coherebot|inflectionbot|google-extended|ccbot|doubaobot|yuanbaobot|hunyuanbot|qwenbot|tongyibot|wenxinbot|moonshotbot|deepseekbot|kimibot|zhipubot|youbot|google|bing|yahoo|yandex|duckduckbot|slurp|applebot|baidu|sogou|bytespider|shenma|youdao|360spider|soso|semrush|ahrefs|petalbot|exabot|facebot|facebook|twitter|linkedin|telegram|ia_archiver/i;
// 兼容UA为空的边界情况,使用test()比match()性能更优
const isSpider = spiderRegex.test(navigator.userAgent || '');
if (isSpider) {
// 爬虫访问:正常渲染页面,不做跳转
// 可在此处补充爬虫专属处理逻辑(如输出专属内容、统计爬虫访问等)
} else {
// 真实用户访问:跳转到指定页面
// 请修改下方路径为你的目标跳转地址
window.location.replace('/error.html');
// 备选方案:隐藏页面并显示提示(依赖jQuery)
// $('body').css('display', 'none');
// $('html').html('<div>404</div>');
}
</script>
版本2:js网站跳转代码(referrer来源页面判断,蜘蛛正常,用户通过搜索引擎进入不跳转,其他任何形式进入全部跳转)(直接粘贴到页面即可,建议放<head>后面直接加载):
<script type="text/javascript">
// 访问控制逻辑:官方蜘蛛正常访问 | 搜索引擎来源正常访问 | AI平台来源正常访问 | 其余访问跳转
// 蜘蛛UA分类顺序:通用泛匹配 > 全球主流AI蜘蛛 > 中国主流AI蜘蛛 > 全球主流搜索引擎 > 中国主流搜索引擎 > 其他类蜘蛛
const spiderRegex = /bot|spider|crawl|crawler|index|archive|gptbot|oai-searchbot|chatgpt-user|claudebot|claude-searchbot|claude-user|xai-searchbot|xai-bot|xai-grok|meta-externalagent|meta-externalfetcher|perplexitybot|perplexity-user|huggingfacebot|amazonbot|coherebot|inflectionbot|google-extended|ccbot|doubaobot|yuanbaobot|hunyuanbot|qwenbot|tongyibot|wenxinbot|moonshotbot|deepseekbot|kimibot|zhipubot|youbot|google|bing|yahoo|yandex|duckduckbot|slurp|applebot|baidu|sogou|bytespider|shenma|youdao|360spider|soso|semrush|ahrefs|petalbot|exabot|facebot|facebook|twitter|linkedin|telegram|ia_archiver/i;
// 搜索引擎来源域名正则 - 匹配全球+中国主流搜索引擎referrer,支持多级域名
const searchReferrerRegex = /\.(google|bing|yahoo|yandex|duckduckgo|apple|baidu|sogou|shenma|sm|youdao|360|soso|toutiao)(\.+){0,2}\//i;
// AI平台来源域名正则 - 匹配全球+中国主流AI平台referrer,支持多级域名
const aiReferrerRegex = /\.(openai|anthropic|claude|xai|perplexity|huggingface|cohere|inflection|meta|gemini|doubao|wenxin|tongyi|qianwen|hunyuan|moonshot|deepseek)(\.+){0,2}\//i;
// 1. 优先判断是否为官方蜘蛛爬虫(蜘蛛不受来源限制,一律放行)
const isSpider = spiderRegex.test(navigator.userAgent || '');
if (isSpider) {
// 爬虫访问:正常渲染页面,不做跳转
// 可在此处补充爬虫专属处理逻辑(如统计爬虫访问量)
} else {
// 2. 普通用户则判断访问来源:搜索引擎 或 AI平台 均放行
const referrer = document.referrer || '';
const isFromSearchEngine = searchReferrerRegex.test(referrer);
const isFromAIPlatform = aiReferrerRegex.test(referrer);
if (!isFromSearchEngine && !isFromAIPlatform) {
// 直接访问 / 非搜索引擎&非AI平台来源:跳转到指定页面
window.location.replace('/error.html');
// 备选方案:隐藏页面并显示提示(依赖jQuery)
// $('body').css('display', 'none');
// $('html').html('<div>404</div>');
}
// 搜索引擎来源 / AI平台来源访客:正常渲染页面
}
</script>
分享一个简单错误提示页面,可用于跳转后显示:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>抱歉,站点已停止</title>
<style>
html,body,div,h1,*{margin:0;padding:0;}
body{background-color:#fefefe;color:#333}
.box{width:580px;margin:0 auto;}
h1{font-size:20px;text-align:center;background:url(背景图链接,不需要则删除) no-repeat top center; padding-top:90px;margin-top:30%;font-weight:normal;}
</style>
</head>
<body>
<div class="box"><h1>抱歉!该站点已经停止运行!</h1></div>
</body>
</html>
页:
[1]