这款工具专门用于生成实现网站永久重定向(301 Redirect)所需的服务器端代码。用户只需输入原始URL和目标URL,工具便会根据所选服务器环境(如Apache的.htaccess、Nginx的nginx.conf、IIS的web.config或PHP代码)自动输出对应的301重定向规则。它能有效避免因网站改版、域名更换或URL结构调整导致的流量损失和搜索引擎排名下降,是网站运维和迁移过程中的核心配置工具。

除了基础的URL对URL重定向,高级工具还支持基于正则表达式的批量重定向规则生成,并能智能处理查询参数的保留与传递。部分生成器还提供重定向链检测和HTTP状态码验证功能,防止出现重定向循环。对于需要大量URL跳转的SEO项目或网站重构,此工具不仅能保证重定向的精准实施,还能通过生成清晰、标准的代码降低服务器配置的复杂度与出错风险。
';
case 'js':
return '// JavaScript 301重定向\nwindow.location.replace("' + newUrl + '");';
default:
return '';
}
}
// 生成批量跳转代码
function generateBatchRedirectCode(pairs, method) {
let output = '';
switch (method) {
case 'apache':
output = '# Apache .htaccess 批量301重定向\n';
pairs.forEach(pair => {
output += 'Redirect 301 ' + pair.oldUrl + ' ' + pair.newUrl + '\n';
});
break;
case 'nginx':
output = '# Nginx 批量301重定向\n';
pairs.forEach(pair => {
output += 'location = ' + pair.oldUrl + ' {\n return 301 ' + pair.newUrl + ';\n}\n\n';
});
break;
case 'php':
output = '<' + '?php\n// PHP 批量301重定向\n';
output += '$redirects = array(\n';
pairs.forEach((pair, index) => {
output += ' "' + pair.oldUrl + '" => "' + pair.newUrl + '"';
if (index < pairs.length - 1) output += ',';
output += '\n';
});
output += ');\n\n';
output += '$currentUrl = $_SERVER["REQUEST_URI"];\n';
output += 'if (isset($redirects[$currentUrl])) {\n';
output += ' header("HTTP/1.1 301 Moved Permanently");\n';
output += ' header("Location: " . $redirects[$currentUrl]);\n';
output += ' exit();\n';
output += '}\n?>';
break;
case 'html':
output = '\n';
output += '';
break;
case 'js':
output = '// JavaScript 批量301重定向\n';
output += 'const redirects = {\n';
pairs.forEach((pair, index) => {
output += ' "' + pair.oldUrl + '": "' + pair.newUrl + '"';
if (index < pairs.length - 1) output += ',';
output += '\n';
});
output += '};\n\n';
output += 'const currentPath = window.location.pathname;\n';
output += 'if (redirects[currentPath]) {\n';
output += ' window.location.replace(redirects[currentPath]);\n';
output += '}';
break;
}
return output;
}
// 验证URL格式
function isValidUrl(url) {
try {
new URL(url);
return true;
} catch {
// 也支持相对路径
return url.startsWith('/') || url.startsWith('./') || url.startsWith('../');
}
}
// 复制代码
function copyRedirect() {
const output = document.getElementById('redirectOutput');
if (!output.value.trim()) {
showMessage('请先生成代码!', 'error');
return;
}
output.select();
output.setSelectionRange(0, 99999);
try {
document.execCommand('copy');
showMessage('代码已复制到剪贴板!', 'success');
} catch (error) {
navigator.clipboard.writeText(output.value).then(() => {
showMessage('代码已复制到剪贴板!', 'success');
}).catch(() => {
showMessage('复制失败,请手动复制', 'error');
});
}
}
// 清空表单
function clearRedirect() {
if (confirm('确定要清空所有内容吗?')) {
document.getElementById('oldUrl').value = '';
document.getElementById('newUrl').value = '';
document.getElementById('batchUrls').value = '';
document.getElementById('redirectOutput').value = '';
document.getElementById('typeSingle').checked = true;
document.getElementById('typeBatch').checked = false;
document.getElementById('singleRedirectForm').style.display = 'block';
document.getElementById('batchRedirectForm').style.display = 'none';
document.getElementById('methodApache').checked = true;
showMessage('已清空所有内容', 'info');
}
}
// 显示消息提示
function showMessage(message, type = 'info') {
const existingToast = document.querySelector('.message-toast');
if (existingToast) {
existingToast.remove();
}
const toast = document.createElement('div');
toast.className = `message-toast ${type}`;
toast.innerHTML = `
${message}
`;
document.body.appendChild(toast);
setTimeout(() => {
toast.classList.add('show');
}, 100);
setTimeout(() => {
toast.classList.remove('show');
setTimeout(() => {
if (toast.parentElement) {
toast.remove();
}
}, 300);
}, 3000);
}
// 页面加载完成后的初始化
document.addEventListener('DOMContentLoaded', function() {
setTimeout(() => {
showMessage('提示:支持单个和批量URL跳转,多种服务器环境', 'info');
}, 1000);
});
评论