Forgot Password
 Register
View: 30|Reply: 0

Rewrite Rules for SEO urls on Discuz! X5.0

[Copy Link]
Posted on 4 days ago | Show all floors |Read Mode
File need to edit: source/function/function_core.php


  1. function rewriteoutput($type, $returntype, $host) {
  2.         global $_G;
  3.         $fextra = '';
  4.         if($type == 'forum_forumdisplay') {
  5.                 [, , , $fid, $page, $extra] = func_get_args();
  6.                 $r = [
  7.                         '{fid}' => empty($_G['setting']['forumkeys'][$fid]) ? $fid : $_G['setting']['forumkeys'][$fid],
  8.                         '{page}' => $page ? $page : 1,
  9.                 ];
  10.         } elseif($type == 'forum_viewthread') {
  11.                 [, , , $tid, $page, $prevpage, $extra] = func_get_args();
  12.                 $r = [
  13.                         '{tid}' => $tid,
  14.                         '{page}' => $page ? $page : 1,
  15.                         '{prevpage}' => $prevpage && !IS_ROBOT ? $prevpage : 1,
  16.                 ];
  17.         } elseif($type == 'home_space') {
  18.                 [, , , $uid, $username, $extra] = func_get_args();
  19.                 $_G['setting']['rewritecompatible'] && $username = rawurlencode($username);
  20.                 $r = [
  21.                         '{user}' => $uid ? 'uid' : 'username',
  22.                         '{value}' => $uid ? $uid : $username,
  23.                 ];
  24.         } elseif($type == 'home_blog') {
  25.                 [, , , $uid, $blogid, $extra] = func_get_args();
  26.                 $r = [
  27.                         '{uid}' => $uid,
  28.                         '{blogid}' => $blogid,
  29.                 ];
  30.         } elseif($type == 'group_group') {
  31.                 [, , , $fid, $page, $extra] = func_get_args();
  32.                 $r = [
  33.                         '{fid}' => $fid,
  34.                         '{page}' => $page ? $page : 1,
  35.                 ];
  36.         } elseif($type == 'portal_topic') {
  37.                 [, , , $name, $extra] = func_get_args();
  38.                 $r = [
  39.                         '{name}' => $name,
  40.                 ];
  41.         } elseif($type == 'portal_article') {
  42.                 [, , , $id, $page, $extra] = func_get_args();
  43.                 $r = [
  44.                         '{id}' => $id,
  45.                         '{page}' => $page ? $page : 1,
  46.                 ];
  47.         } elseif($type == 'forum_archiver') {
  48.                 [, , $action, $value, $page, $extra] = func_get_args();
  49.                 $host = '';
  50.                 $r = [
  51.                         '{action}' => $action,
  52.                         '{value}' => $value,
  53.                 ];
  54.                 if($page) {
  55.                         $fextra = '?page='.$page;
  56.                 }
  57.         } elseif($type == 'plugin') {
  58.                 [, , $pluginid, $module, , $param, $extra] = func_get_args();
  59.                 $host = '';
  60.                 $r = [
  61.                         '{pluginid}' => $pluginid,
  62.                         '{module}' => $module,
  63.                 ];
  64.                 if($param) {
  65.                         $fextra = '?'.$param;
  66.                 }
  67.         }
  68.         $href = str_replace(array_keys($r), $r, $_G['setting']['rewriterule'][$type]).$fextra;
  69.         if(!$returntype) {
  70.                 return '<a href="'.$host.$href.'"'.(!empty($extra) ? stripslashes($extra) : '').'>';
  71.         } else {
  72.                 return $host.$href;
  73.         }
  74. }
Copy Code
Change these code into:
  1. function rewriteoutput($type, $returntype, $host) {
  2.         global $_G;
  3.         $fextra = '';
  4.        
  5.         // ========== SEO URLs for Discuz Latin characters title==========
  6.         $seo_string = function($string, $max_length = 80) {
  7.                 if(empty($string)) return '';

  8.                 $string = preg_replace('/[^a-zA-Z0-9\s\-\p{L}]/u', '', $string);
  9.                 $string = preg_replace('/[\s\-]+/', '-', $string);
  10.                 $string = trim($string, '-');
  11.                 $string = mb_strtolower($string, 'UTF-8');

  12.                 if($max_length > 0 && mb_strlen($string, 'UTF-8') > $max_length) {
  13.                         $string = mb_substr($string, 0, $max_length, 'UTF-8');
  14.                         $string = rtrim($string, '-');
  15.                 }
  16.                 return $string;
  17.         };
  18.         // =========================================================
  19.        
  20.         if($type == 'forum_forumdisplay') {
  21.                 list(,,, $fid, $page, $extra) = func_get_args();

  22.                 $forum_name = DB::result_first("SELECT name FROM " . DB::table('forum_forum') . " WHERE fid='" . dintval($fid) . "'");
  23.                 $forum_seo = $seo_string($forum_name, 50);
  24.                
  25.                 $r = array(
  26.                         '{fid}' => empty($_G['setting']['forumkeys'][$fid]) ? $fid : $_G['setting']['forumkeys'][$fid],
  27.                         '{page}' => $page ? $page : 1,
  28.                         '{forumname}' => $forum_seo,
  29.                 );
  30.         } elseif($type == 'forum_viewthread') {
  31.                 list(,,, $tid, $page, $prevpage, $extra) = func_get_args();
  32.                 $thread_subject = DB::result_first("SELECT subject FROM " . DB::table('forum_thread') . " WHERE tid='" . dintval($tid) . "'");
  33.                 $thread_seo = $seo_string($thread_subject, 80);
  34.                
  35.                 $r = array(
  36.                         '{tid}' => $tid,
  37.                         '{page}' => $page ? $page : 1,
  38.                         '{prevpage}' => $prevpage && !IS_ROBOT ? $prevpage : 1,
  39.                         '{subject}' => $thread_seo,  //
  40.                 );
  41.         } elseif($type == 'home_space') {
  42.                 list(,,, $uid, $username, $extra) = func_get_args();
  43.                 $_G['setting']['rewritecompatible'] && $username = rawurlencode($username);
  44.                 $r = array(
  45.                         '{user}' => $uid ? 'uid' : 'username',
  46.                         '{value}' => $uid ? $uid : $username,
  47.                 );
  48.         } elseif($type == 'home_blog') {
  49.                 list(,,, $uid, $blogid, $extra) = func_get_args();
  50.                 $r = array(
  51.                         '{uid}' => $uid,
  52.                         '{blogid}' => $blogid,
  53.                 );
  54.         } elseif($type == 'group_group') {
  55.                 list(,,, $fid, $page, $extra) = func_get_args();
  56.                 $r = array(
  57.                         '{fid}' => $fid,
  58.                         '{page}' => $page ? $page : 1,
  59.                 );
  60.         } elseif($type == 'portal_topic') {
  61.                 list(,,, $name, $extra) = func_get_args();
  62.                 $r = array(
  63.                         '{name}' => $name,
  64.                 );
  65.         } elseif($type == 'portal_article') {
  66.                 list(,,, $id, $page, $extra) = func_get_args();

  67.                 $article_title = DB::result_first("SELECT title FROM " . DB::table('portal_article_content') . " WHERE aid='" . dintval($id) . "'");
  68.                 $article_seo = $seo_string($article_title, 80);
  69.                
  70.                 $r = array(
  71.                         '{id}' => $id,
  72.                         '{page}' => $page ? $page : 1,
  73.                         '{title}' => $article_seo,
  74.                 );
  75.         } elseif($type == 'forum_archiver') {
  76.                 list(,, $action, $value, $page, $extra) = func_get_args();
  77.                 $host = '';
  78.                 $r = array(
  79.                         '{action}' => $action,
  80.                         '{value}' => $value,
  81.                 );
  82.                 if($page) {
  83.                         $fextra = '?page='.$page;
  84.                 }
  85.         } elseif($type == 'plugin') {
  86.                 list(,, $pluginid, $module,, $param, $extra) = func_get_args();
  87.                 $host = '';
  88.                 $r = array(
  89.                         '{pluginid}' => $pluginid,
  90.                         '{module}' => $module,
  91.                 );
  92.                 if($param) {
  93.                         $fextra = '?'.$param;
  94.                 }
  95.         }
  96.         $href = str_replace(array_keys($r), $r, $_G['setting']['rewriterule'][$type]).$fextra;
  97.         if(!$returntype) {
  98.                 return '<a href="'.$host.$href.'"'.(!empty($extra) ? stripslashes($extra) : '').'>';
  99.         } else {
  100.                 return $host.$href;
  101.         }
  102. }
Copy Code
and Here is the rewrite rules for NGINX and APACHE servers:
NGINX



  1. <div>#
  2. RewriteRule ^forum-([a-zA-Z0-9\-]+)-([0-9]+)-([0-9]+)\.html$ forum.php?mod=forumdisplay&fid=$2&page=$3 [L,QSA]

  3. #
  4. RewriteRule ^thread-([0-9]+)-([a-zA-Z0-9\-]+)-([0-9]+)\.html$ forum.php?mod=viewthread&tid=$1&page=$3 [L,QSA]

  5. #
  6. RewriteRule ^forum-([0-9]+)-([0-9]+)\.html$ forum.php?mod=forumdisplay&fid=$1&page=$2 [L,QSA]
  7. RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ forum.php?mod=viewthread&tid=$1&page=$2 [L,QSA]</div>
Copy Code
APACHE:


  1. <div>RewriteRule ^forum-([a-zA-Z0-9\-]+)-([0-9]+)-([0-9]+)\.html$ forum.php?mod=forumdisplay&fid=$2&page=$3 [L,QSA]

  2. RewriteRule ^thread-([0-9]+)-([a-zA-Z0-9\-]+)-([0-9]+)\.html$ forum.php?mod=viewthread&tid=$1&page=$3 [L,QSA]

  3. RewriteRule ^forum-([0-9]+)-([0-9]+)\.html$ forum.php?mod=forumdisplay&fid=$1&page=$2 [L,QSA]
  4. RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ forum.php?mod=viewthread&tid=$1&page=$2 [L,QSA]</div>
Copy Code


Reply

Use Props Report

You need to log in before you can reply Login | Register

Forum Credit Rules

Close

Editors SelectedPrevious /2 Next

Archiver|Mobile|Darkroom|Privacy|DSC

GMT+7, 2026-04-17 15:47 , Processed in 0.009433 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

Quick Reply Back to Top Return to List