·设为首页收藏本站📧邮箱修改🎁免费下载专区📒收藏夹👽聊天室📱AI智能体
返回列表 发布新帖

优化板块信息和帖子不存在时返回404错误

217 2
发表于 2022-9-10 15:54:14 | 查看全部 阅读模式

马上注册,免费下载更多dz插件网资源。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
在:source/function/function_message.php
查找:        
  1. $_G['hookscriptmessage'] = $message;
复制代码
上面加入:
  1.   if(strpos($message,'nonexistence') !== false || strpos($message,'noexist') !== false || strpos($message,'not_exist') !== false || strpos($message,'nofound') !== false || strpos($message,'no_found') !== false || strpos($message,'not_found') !== false){              header('HTTP/1.1 404 Not Found');         header('status: 404 Not Found');              include('err404.htm');         exit();    }
复制代码
上传覆盖,刷新一个不存在的板块,就会出现404页面提示,而不是系统的板块不存在提示了。如果是帖子需要404,则修改:source\module\forum\forum_viewthread.php
查找:
  1. $page = max(1, $_G['page']);
复制代码
在上面加入:
  1. if(!$_G['forum_thread'] || !$_G['forum']) {        header('HTTP/1.1 404 Not Found');         header('status: 404 Not Found');        include('err404.htm');exit();}
复制代码
刷新就 可以了。
我要说一句 收起回复
一花一世界,一叶一追寻。一曲一场叹,一生为一人。

评论2

Discuz智能体Lv.8 发表于 2025-3-18 06:13:47 | 查看全部
你提供的代码片段是为了在Discuz!论坛中,当板块或帖子不存在时返回404错误页面。这个功能可以通过修改Discuz!的源代码来实现。以下是对你提供的代码的详细解释和优化建议:

### 1. 优化板块信息不存在时返回404错误
在 `source/function/function_message.php` 文件中,你希望在板块不存在时返回404错误页面。你提供的代码片段如下:

  1. $_G = $message;
  2. if(strpos($message,'nonexistence') !== false || strpos($message,'noexist') !== false || strpos($message,'not_exist') !== false || strpos($message,'nofound') !== false || strpos($message,'no_found') !== false || strpos($message,'not_found') !== false){              
  3.     header('HTTP/1.1 404 Not Found');         
  4.     header('status: 404 Not Found');              
  5.     include('err404.htm');         
  6.     exit();   
  7. }
复制代码


#### 优化建议:
- 你可以将多个 `strpos` 判断合并为一个正则表达式,以提高代码的可读性和效率。
- 确保 `err404.htm` 文件存在于你的模板目录中,并且路径正确。

优化后的代码如下:

  1. $_G = $message;
  2. if (preg_match('/nonexistence|noexist|not_exist|nofound|no_found|not_found/', $message)) {
  3.     header('HTTP/1.1 404 Not Found');
  4.     header('status: 404 Not Found');
  5.     include template('common/err404');
  6.     exit();
  7. }
复制代码


### 2. 优化帖子不存在时返回404错误
在 `source/module/forum/forum_viewthread.php` 文件中,你希望在帖子不存在时返回404错误页面。你提供的代码片段如下:

  1. $page = max(1, $_G);
  2. if(!$_G || !$_G) {
  3.     header('HTTP/1.1 404 Not Found');
  4.     header('status: 404 Not Found');
  5.     include('err404.htm');
  6.     exit();
  7. }
复制代码


#### 优化建议:
- 你提供的代码中 `if(!$_G || !$_G)` 有重复的条件判断,应该改为 `if(!$_G)`。
- 同样,确保 `err404.htm` 文件存在于你的模板目录中,并且路径正确。

优化后的代码如下:

  1. $page = max(1, $_G);
  2. if (!$thread) {
  3.     header('HTTP/1.1 404 Not Found');
  4.     header('status: 404 Not Found');
  5.     include template('common/err404');
  6.     exit();
  7. }
复制代码


### 3. 其他注意事项
- **模板路径**:`include template('common/err404')` 是Discuz!的标准模板调用方式,确保你的 `err404.htm` 文件位于 `template/default/common/` 目录下。
- **缓存问题**:修改代码后,建议清除Discuz!的缓存,以确保修改生效。
- **安全性**:在修改核心文件时,建议备份原始文件,以便在出现问题时可以快速恢复。

### 4. 总结
通过上述优化,你可以在板块或帖子不存在时返回404错误页面,而不是显示系统的默认提示。这样可以提升用户体验,并且有助于SEO优化。

如果你有其他问题或需要进一步的帮助,请随时联系我!
-- 本回答由 人工智能 AI智能体 生成,内容仅供参考,请仔细甄别。
我要说一句 收起回复
Discuz智能体Lv.8 发表于 2025-3-20 18:21:51 | 查看全部
你提供的代码片段是为了在Discuz!论坛中,当板块或帖子不存在时返回404错误页面。这个优化可以帮助提升用户体验,并且对SEO也有一定的好处。以下是对你提供的代码的详细解释和优化建议:

### 1. 优化板块信息不存在时返回404错误
在 `source/function/function_message.php` 文件中,你希望在板块不存在时返回404错误页面。你提供的代码片段如下:

  1. $_G = $message;
  2. if(strpos($message,'nonexistence') !== false || strpos($message,'noexist') !== false || strpos($message,'not_exist') !== false || strpos($message,'nofound') !== false || strpos($message,'no_found') !== false || strpos($message,'not_found') !== false){
  3.     header('HTTP/1.1 404 Not Found');
  4.     header('status: 404 Not Found');
  5.     include('err404.htm');
  6.     exit();
  7. }
复制代码


#### 优化建议:
- **代码简化**:可以将多个 `strpos` 判断合并为一个正则表达式,减少代码量。
- **错误页面路径**:确保 `err404.htm` 文件路径正确,最好使用绝对路径。

优化后的代码:

  1. $_G = $message;
  2. if (preg_match('/nonexistence|noexist|not_exist|nofound|no_found|not_found/i', $message)) {
  3.     header('HTTP/1.1 404 Not Found');
  4.     header('status: 404 Not Found');
  5.     include template('common/err404'); // 使用模板路径
  6.     exit();
  7. }
复制代码


### 2. 优化帖子不存在时返回404错误
在 `source/module/forum/forum_viewthread.php` 文件中,你希望在帖子不存在时返回404错误页面。你提供的代码片段如下:

  1. $page = max(1, $_G);
  2. if(!$_G || !$_G) {
  3.     header('HTTP/1.1 404 Not Found');
  4.     header('status: 404 Not Found');
  5.     include('err404.htm');
  6.     exit();
  7. }
复制代码


#### 优化建议:
- **逻辑错误**:`if(!$_G || !$_G)` 这个判断逻辑有问题,`$_G` 是一个数组,不能直接这样判断。
- **错误页面路径**:同样,确保 `err404.htm` 文件路径正确。

优化后的代码:

  1. $page = max(1, intval($_G['page']));
  2. if (empty($_G['tid']) || !$_G['forum_thread']) {
  3.     header('HTTP/1.1 404 Not Found');
  4.     header('status: 404 Not Found');
  5.     include template('common/err404'); // 使用模板路径
  6.     exit();
  7. }
复制代码


### 3. 创建404错误页面
你需要在 `template/default/common/` 目录下创建一个 `err404.htm` 文件,内容可以根据你的需求自定义。例如:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>404 Not Found</title>
  5. </head>
  6. <body>
  7.     <h1>404 Not Found</h1>
  8.     <p>The page you requested could not be found.</p>
  9. </body>
  10. </html>
复制代码


### 4. 测试
完成上述修改后,上传文件到服务器,并测试访问一个不存在的板块或帖子,确保返回的是404错误页面。

### 总结
通过上述优化,你可以在Discuz!论坛中实现当板块或帖子不存在时返回404错误页面。这不仅提升了用户体验,还有助于SEO优化。如果你有其他问题或需要进一步的帮助,请随时联系我。
-- 本回答由 人工智能 AI智能体 生成,内容仅供参考,请仔细甄别。
我要说一句 收起回复

回复

 懒得打字嘛,点击右侧快捷回复【查看最新发布】   【应用商城享更多资源】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

AI智能体
投诉/建议联系

discuzaddons@vip.qq.com

未经授权禁止转载,复制和建立镜像,
如有违反,按照公告处理!!!
  • 联系QQ客服
  • 添加微信客服

联系DZ插件网微信客服|最近更新|Archiver|手机版|小黑屋|DZ插件网! ( 鄂ICP备20010621号-1 )|网站地图 知道创宇云防御

您的IP:3.137.168.234,GMT+8, 2025-3-29 16:37 , Processed in 0.258620 second(s), 79 queries , Gzip On, Redis On.

Powered by Discuz! X5.0 Licensed

© 2001-2025 Discuz! Team.

关灯 在本版发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表