无.htaccess时通过PHP实现旧论坛URL定向至新链接
解决方案
1. 创建PHP重定向文件
在你的/forums目录下新建index.php文件,写入以下代码:
<?php // 获取当前请求的完整路径(例:/forums/canon/182027-firmware-c5550i.html) $requestPath = $_SERVER['REQUEST_URI']; // 用正则提取URL里的6位线程ID if (preg_match('/\/forums\/.*?(\d{6})-/', $requestPath, $matches)) { $threadId = $matches[1]; // 构建目标跳转链接 $targetUrl = "https://www.mywebsite.com/showthread.php?t=$threadId"; // 发送301永久重定向(对SEO友好,告诉搜索引擎地址永久变更) header("HTTP/1.1 301 Moved Permanently"); header("Location: $targetUrl"); exit; } else { // 未找到有效ID时返回404 header("HTTP/1.1 404 Not Found"); echo "页面未找到"; exit; } ?>
2. 配置请求转发(关键步骤)
因为旧URL格式是/forums/xxx/xxx.html,默认服务器会尝试访问对应子目录和文件,所以需要让所有/forums/开头的请求都指向刚才创建的index.php:
- 登录你的云托管平台后台,找到路由规则或URL重写设置
- 添加规则:来源路径设为
/forums/*,目标路径设为/forums/index.php - 保存规则后,服务器会自动把所有
/forums/下的请求转发到PHP文件处理
3. 验证测试
手动访问旧格式URL(比如https://www.mywebsite.com/forums/canon/182027-firmware-c5550i.html),确认是否自动跳转到https://www.mywebsite.com/showthread.php?t=182027。
内容的提问来源于stack exchange,提问作者JimHegs
相关产品推荐
相关产品推荐

