Jump to content

Sitemap XML for WHMCS Knowledgebase


thalhah

Recommended Posts

Hi, I create my own sitemap.xml for WHMCS Knowledgebase. here's the code.

 

<?php 

//  #######################  SET WHMCS ENVIRONMENT  ########################
   use WHMCS\ClientArea;
   define('CLIENTAREA', true);
   require_once("init.php");
   require_once("includes/functions.php");
   require_once("includes/clientareafunctions.php");

//  #########################  CONNECT TO DATABASE  ########################
   $db = mysqli_connect($db_host, $db_username, $db_password, $db_name);
   if (mysqli_connect_errno())
   {
       echo "Failed to connect to Database: " . mysqli_connect_error();
   }

$sql_whmcs = mysqli_query($db,"SELECT * FROM tblconfiguration WHERE setting='SystemURL' LIMIT 1"); 
$rlt_whmcs = mysqli_fetch_array($sql_whmcs);
$CFG["siteurl"] = substr($rlt_whmcs["value"], 0, -1);

header('Content-Type: application/xml; charset=utf-8');
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n"; 

$result = mysqli_query($db,"SELECT * FROM tblknowledgebase"); 
while ($data = mysqli_fetch_array($result)) { 
$title = str_replace(array(' ','/'), '-', $data['title']);    
$title = str_replace(array('?','.','[',']','"'), '', $title); 
echo '<url>'."\n"; 
echo '<loc>'.$CFG["siteurl"].'/knowledgebase/'.$data['id'].'/'.$title.'.html'.'</loc>'."\n"; 
echo '</url>'."\n";     
}//while 

echo '</urlset>'."\n"; 
?>

Link to comment
Share on other sites

  • 1 year later...
  • 8 months later...

Have managed to get the code submitted by thalhah to work with some minor modifications:

<?php header('Content-type: application/xml; charset=utf-8') ?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>' ?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

<?php 

//  #######################  SET WHMCS ENVIRONMENT  ########################
   use WHMCS\ClientArea;
   define('CLIENTAREA', true);
   require_once("init.php");
   require_once("includes/functions.php");
   require_once("includes/clientareafunctions.php");

//  #########################  CONNECT TO DATABASE  ########################
   $db = mysqli_connect($db_host, $db_username, $db_password, $db_name);
   if (mysqli_connect_errno())
   {
       echo "Failed to connect to Database: " . mysqli_connect_error();
   }

$sql_whmcs = mysqli_query($db,"SELECT * FROM tblconfiguration WHERE setting='SystemURL' LIMIT 1"); 
$rlt_whmcs = mysqli_fetch_array($sql_whmcs);
$CFG["siteurl"] = substr($rlt_whmcs["value"], 0, -1);

$result = mysqli_query($db,"SELECT * FROM tblknowledgebase"); 
while ($data = mysqli_fetch_array($result)) { 
$title = str_replace(array(' ','/'), '-', $data['title']);    
$title = str_replace(array('?','.','[',']','"'), '', $title); 
echo '  <url>'."\n"; 
echo '      <loc>'.$CFG["siteurl"].'/knowledgebase/'.$data['id'].'/'.$title.'.html'.'</loc>'."\n"; 
echo '  </url>'."<br>";     
}//while 

echo '</urlset>'."\n"; 
?>

Save this as sitemap.php in your WHMCS root folder within your public_html directory, i.e. public_html/whmcs/sitemap.php

This will now correctly output each Knowledgebase article's URL on a separate line within XML format. 

You can now add the URL of your sitemap.php file to Google and Bing Webmasters as it is, no need to use a .htaccess rewrite rule to convert sitemap.xml to sitemap.php unless you really want to. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated