為避免外部鏈接直接傳遞頁面權重、洩露引用來源、或需統計點擊數據,通過中間頁 go.php 實現重定向是經典方案。本文提供完整實現代碼、Emlog/WordPress 接入示例、安全加固與 SEO 建議。
原始外鏈:<a href="https://target.com">目標站</a>
重定向後:<a href="/go.php?url=https://target.com">目標站</a>
/go.php?url=... → 服務端驗證、記錄日誌 → 302/301 Location: https://target.com → 瀏覽器跳轉。/go.php?url=...,配合 robots.txt 禁止抓取,避免權重流失。go.php 完整實現(支援直跳/框架跳、域名白名單、點擊統計)<?php
// go.php
define('GO_LOG_FILE', __DIR__ . '/logs/go_redirect.log');
// 1. 獲取並驗證目標 URL
$rawUrl = $_GET['url'] ?? '';
if (!$rawUrl) {
header('HTTP/1.0 400 Bad Request');
exit('Missing url parameter');
}
// URL 解碼(兼容雙重編碼)
$url = rawurldecode($rawUrl);
$url = rawurldecode($url);
// 2. 安全校驗:協議、域名白名單、長度
$allowedSchemes = ['http', 'https'];
$parsed = @parse_url($url);
if (!$parsed || !in_array($parsed['scheme'] ?? '', $allowedSchemes)) {
header('HTTP/1.0 400 Bad Request');
exit('Invalid URL scheme');
}
// 可選:域名白名單(防止開放重定向漏洞被釣魚利用)
$allowedDomains = ['example.com', 'trusted-partner.com']; // 配置你的信任域名
$host = strtolower($parsed['host'] ?? '');
$domainOk = false;
foreach ($allowedDomains as $d) {
if ($host === $d || preg_match('/\.' . preg_quote($d, '/') . '$/', $host)) {
$domainOk = true; break;
}
}
if (!$domainOk && !empty($allowedDomains)) {
// 不在白名單:顯示中間確認頁,而非直接跳轉
showConfirmPage($url);
exit;
}
// 3. 記錄點擊日誌(異步寫入不阻塞)
$log = sprintf(
"[%s] IP:%s UA:%s REF:%s URL:%s\n",
date('Y-m-d H:i:s'),
$_SERVER['REMOTE_ADDR'] ?? 'unknown',
$_SERVER['HTTP_USER_AGENT'] ?? 'unknown',
$_SERVER['HTTP_REFERER'] ?? 'direct',
$url
);
@file_put_contents(GO_LOG_FILE, $log, FILE_APPEND | LOCK_EX);
// 4. 執行跳轉
$directGo = true; // true=直接 302,false=框架頁(舊版兼容)
if ($directGo) {
// 302 臨時重定向(不傳遞權重);若需永久用 301
header("Location: $url", true, 302);
exit;
} else {
// 框架跳轉模式(保留頂部導航/統計碼)
echo '<!DOCTYPE html><html><head><meta charset="utf-8"><title>正在跳轉...</title></head><body>';
echo '<script>window.location.href = ' . json_encode($url) . ';</script>';
echo '<p>正在跳轉至 <a href="' . htmlspecialchars($url) . '">' . htmlspecialchars($url) . '</a>...</p>';
echo '</body></html>';
}
function showConfirmPage($url) {
$host = parse_url($url, PHP_URL_HOST);
echo '<!DOCTYPE html><html><head><meta charset="utf-8"><title>外部鏈接提示</title><style>body{font-family:sans-serif;max-width:600px;margin:50px auto;padding:20px;text-align:center;}</style></head><body>';
echo "<h2>您將訪問外部站點</h2>";
echo "<p>目標域名:<strong>" . htmlspecialchars($host) . "</strong></p>";
echo "<p><a href=\"" . htmlspecialchars($url) . "\" class=\"btn\">繼續訪問</a> <a href=\"javascript:history.back()\" class=\"btn\">取消</a></p>";
echo '</body></html>';
}
module.php)// 原評論者鏈接輸出代碼 // <a href="" target="_blank"> // 修改為 <a href="/go.php?url=" target="_blank" rel="external nofollow noopener">
// 過濾評論作者鏈接
add_filter('get_comment_author_link', function($link) {
// 正則提取 href,重構
return preg_replace_callback('/href="([^"]+)"/', function($m) {
return 'href="/go.php?url=' . urlencode($m[1]) . '" rel="external nofollow noopener"';
}, $link);
});
// 過濾文章內容外鏈(可選)
add_filter('the_content', function($content) {
return preg_replace_callback('/<a\s+href="(https?:\/\/[^"]+)"([^>]*)>/i', function($m) {
// 可加域名白名單判斷,排除本站鏈接
return '<a href="/go.php?url=' . urlencode($m[1]) . '"' . $m[2] . ' rel="external nofollow noopener">';
}, $content);
});
User-agent: *
Disallow: /go.phprel="nofollow noopener",防權重傳遞與 window.opener 安全風險。showConfirmPage),否則攻擊者可構造 your.com/go.php?url=phishing.com 進行釣魚。// 生成帶統計參數的短鏈接 // 1. 記錄 mapping 表:short_code -> target_url + create_time + clicks // 2. 前端輸出:/go/abc123 // 3. go.php 解析 short_code,查表,clicks++,302 跳轉 // 優點:URL 更乾淨、可追蹤具體活動來源(UTM 參數自動附加)