SEO超级伪静态 目录化伪静态后,有时点击跳转到错误链接的解决办法
部分插件跳转时,没带上网站域名,然后浏览器就默认以当前url路径为前缀进行跳转,导致链接错误打开:source\function\function_core.php
找到
function dheader($string, $replace = true, $http_response_code = 0) { $islocation = substr(strtolower(trim($string)), 0, 8) == 'location'; if(defined('IN_MOBILE') && strpos($string, 'mobile') === false && $islocation) { if (strpos($string, '?') === false) { $string = $string.'?mobile='.IN_MOBILE; } else { if(strpos($string, '#') === false) { $string = $string.'&mobile='.IN_MOBILE; } else { $str_arr = explode('#', $string); $str_arr = $str_arr.'&mobile='.IN_MOBILE; $string = implode('#', $str_arr); } } } $string = str_replace(array("\r", "\n"), array('', ''), $string); if(empty($http_response_code) || PHP_VERSION < '4.3' ) { @header($string, $replace); } else { @header($string, $replace, $http_response_code); } if($islocation) { exit(); }}修改为function dheader($string, $replace = true, $http_response_code = 0) { global $_G; $islocation = substr(strtolower(trim($string)), 0, 8) == 'location'; if($islocation){ if(!preg_match('/location:\s*https?:\/\//is', $string)){ $string = preg_replace("/location:\s*/is", 'Location: '.$_G['siteurl'], $string); } } if(defined('IN_MOBILE') && strpos($string, 'mobile') === false && $islocation) { if (strpos($string, '?') === false) { $string = $string.'?mobile='.IN_MOBILE; } else { if(strpos($string, '#') === false) { $string = $string.'&mobile='.IN_MOBILE; } else { $str_arr = explode('#', $string); $str_arr = $str_arr.'&mobile='.IN_MOBILE; $string = implode('#', $str_arr); } } } $string = str_replace(array("\r", "\n"), array('', ''), $string); if(empty($http_response_code) || PHP_VERSION < '4.3' ) { @header($string, $replace); } else { @header($string, $replace, $http_response_code); } if($islocation) { exit(); }} 如果你的插件没问题,只是帖子偶尔打开连接错误,可以考虑下边的方法
打开 source\module\forum\forum_viewthread.php
找到
dheader("Location:forum.php?mod=viewthread&tid=$_G");
改成
dheader("Location:$_Gforum.php?mod=viewthread&tid=$_G");
这是discuz自身对“主题有楼层不准确时”的一个操作,跳转是为了处理后刷新页面,但跳转时没带上域名,导致跳转错误 你提到的这个问题是由于在伪静态或插件跳转时,URL没有带上完整的域名,导致浏览器基于当前路径进行跳转,从而出现错误的链接。你提供的解决方案是通过修改 `function_core.php` 文件中的 `dheader` 函数来确保跳转时带上完整的域名。
### 问题分析
在伪静态或插件跳转时,如果跳转的URL没有带上完整的域名(如 `http://` 或 `https://`),浏览器会默认以当前页面的路径为基础进行跳转。例如,如果当前页面是 `https://www.example.com/forum/`,而跳转的URL是 `/thread-123.html`,浏览器会将其解析为 `https://www.example.com/forum/thread-123.html`,这可能导致404错误或其他问题。
### 解决方案
你提供的代码修改方案是正确的,通过检查 `Location` 头是否包含完整的域名,如果没有,则自动补全域名。这样可以避免跳转时出现路径错误的问题。
### 代码解释
1. **全局变量 `$_G`**:`$_G` 是 Discuz! 的全局变量,通常包含站点的基本配置信息,如站点URL等。
2. **正则表达式检查**:通过 `preg_match` 检查 `Location` 头是否包含完整的域名(`http://` 或 `https://`)。
3. **补全域名**:如果 `Location` 头没有包含完整的域名,则通过 `preg_replace` 补全域名。
### 修改后的代码
function dheader($string, $replace = true, $http_response_code = 0) {
global $_G;
$islocation = substr(strtolower(trim($string)), 0, 8) == 'location';
if($islocation){
if(!preg_match('/location:\s*https?:\/\//is', $string)){
$string = preg_replace("/location:\s*/is", 'Location: '.$_G['siteurl'], $string);
}
}
if(defined('IN_MOBILE') && strpos($string, 'mobile') === false && $islocation) {
if (strpos($string, '?') === false) {
$string = $string.'?mobile='.IN_MOBILE;
} else {
if(strpos($string, '#') === false) {
$string = $string.'&mobile='.IN_MOBILE;
} else {
$str_arr = explode('#', $string);
$str_arr = $str_arr.'&mobile='.IN_MOBILE;
$string = implode('#', $str_arr);
}
}
}
$string = str_replace(array("\r", "\n"), array('', ''), $string);
if(empty($http_response_code) || PHP_VERSION < '4.3' ) {
@header($string, $replace);
} else {
@header($string, $replace, $http_response_code);
}
if($islocation) {
exit();
}
}
### 注意事项
1. **备份文件**:在修改 `function_core.php` 文件之前,请务必备份原始文件,以防止修改错误导致站点无法正常运行。
2. **测试环境**:建议在测试环境中先进行修改和测试,确保没有问题后再应用到生产环境。
3. **插件兼容性**:某些插件可能会依赖原始的 `dheader` 函数,修改后可能会影响这些插件的正常运行。如果发现问题,可以尝试与插件开发者沟通,或者根据具体情况进行调整。
### 其他建议
如果你使用的是第三方插件导致的跳转问题,建议检查插件的代码,确保跳转时使用完整的URL。如果插件代码不可修改,可以考虑在插件跳转时通过钩子(hook)或事件(event)来修正URL。
希望这些信息对你有所帮助!如果还有其他问题,欢迎继续提问。
-- 本回答由 人工智能 AI智能体 生成,内容仅供参考,请仔细甄别。
页:
[1]