Hallo! Selamat Datang di Marketplace produk digital, Freelancer terbaik di indonesia (68495 Members, 790 Products, 5357 Topic, 13048 Comments)

Cara Membuat Sitemap pada Project Codeigniter, Kasus CMS Swarakalibata

Dibuat
Login Terakhir 19 Jam lalu,
Telah Dilihat 3025 Kali
PHPMU.COM Support Mengatakan : Sultan Account
  1. Assalamualaikum Warahmatullahi Wabarakatuh...
  2. Berikut tahapan mudah membuat sitemap pada project codeigniter untuk keperluan submit ke google search console :
  3.  
  4. 1. Buka file : application\config\routes.php
  5. Dibagian paling bawah Tambahkan baris :
  6. $route['sitemap\.xml'] = 'sitemap';
  7.  
  8. 2. Buat Sebuah file pada folder "application/controllers" dengan nama "Sitemap.php" isikan dengan script berikut :
  9. <?php
  10. defined('BASEPATH') OR exit('No direct script access allowed');
  11. class Sitemap extends CI_Controller {
  12. public function index(){
  13. $data['sitemap'] = $this->model_app->view_ordering('berita','id_berita','DESC',0,10);
  14. $this->load->view('administrator/sitemap',$data);
  15. }
  16. }
  17.  
  18. 3. Buat sebuah file baru "sitemap.php" pada folder : application\views\administrator\sitemap.php isi dengan script berikut :
  19. <?php
  20. header('Content-type: application/xml; charset="ISO-8859-1"',true);
  21. ?>
  22. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  23. <url>
  24. <loc><?php echo base_url() ?></loc>
  25. <lastmod><?php echo date('d-m-Y H:i:s') ?></lastmod>
  26. <changefreq>daily</changefreq>
  27. <priority>0.1</priority>
  28. </url>
  29. <?php foreach ($sitemap as $row) { ?>
  30. <url>
  31. <loc><?php echo base_url()."berita/detail/$row[judul_seo]"; ?></loc>
  32. <lastmod><?php echo $row['tanggal'].' '.$row['jam']; ?></lastmod>
  33. <changefreq>daily</changefreq>
  34. <priority>0.1</priority>
  35. </url>
  36. <?php } ?>
  37. </urlset>
  38.  
  39. Oke beres, simpan semuanya dan sekarang coba akses di alamat : http://domain.com/sitemap.xml

Ada 3 Jawaban

Edgar Zeus Polanda
commented on 02 Okt 2020 02:46:21
  1. Assalamualaikum,.. Maaf mas ketika saya membuat semuanya dan selesai menyimpannya, tetapi saat saya akses http://namadomain.com/sitemap.xml
  2. malah kembali ke http://namadomain.com/main
  3. udah benar seperti itu mas? atau ada kesalahan. mohon bantuannya mas.
Naufal Khairuddin
commented on 06 Okt 2020 02:29:19
  1. Assalamualaikum,.. Kak @edgar.. Dijalankan dlu kak http://namadomain.anda/administrator/sitemap
  2.  
  3. ada cara lain juga untuk membuat sitemap, sama seperti membuat rss. Sekalian bisa pake template juga kak, siapa tau bisa berkreasi sendiri. Caranya:
  4. 1. Buka file application\views\administrator\Administrator.php
  5. pada baris 375: function listberita, tambahkan:
  6. $data['sitemap'] = $this->model_utama->view_joinn('berita','users','kategori','username','id_kategori','id_berita','DESC',0,10);
  7. $this->load->view('administrator/sitemap',$data);
  8.  
  9. 2. Buat sebuah file baru "sitemap.php" pada folder: application\views\administrator\sitemap.php isi dengan script berikut:
  10. <?php
  11. $file = fopen("sitemap.xml", "w");
  12. fwrite($file, '<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="'.base_url().'asset/main-sitemap.xsl"?>
  13. <urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">');
  14. fwrite($file, " <url>
  15. <loc>$iden[url]</loc>
  16. <lastmod>2020-10-06 | 05:21:00 </lastmod>
  17. </url>");
  18.  
  19. foreach ($sitemap->result_array() as $row) {
  20. fwrite($file, " <url>
  21. <loc>".base_url()."$row[judul_seo]</loc>
  22. <lastmod>$row[tanggal] | $row[jam]</lastmod>
  23. <image:image>
  24. <image:loc>".base_url()."asset/foto_berita/".$row['gambar']."</image:loc>
  25. <image:title>".cetak_meta($row['judul'],0,255)."</image:title>
  26. </image:image></url>");
  27. }
  28. fwrite($file, "</urlset>");
  29. fclose($file);
  30. ?>
  31.  
  32. 3. Buat sebuah file baru "main-sitemap.xsl" pada folder: asset/main-sitemap.xsl isi dengan script berikut:
  33. <?xml version="1.0" encoding="UTF-8"?>
  34. <xsl:stylesheet version="2.0"
  35. xmlns:html="http://www.w3.org/TR/REC-html40"
  36. xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
  37. xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"
  38. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  39. <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
  40. <xsl:template match="/">
  41. <html xmlns="http://www.w3.org/1999/xhtml">
  42. <head>
  43. <title>XML Sitemap</title>
  44. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  45. <style type="text/css">
  46. body {font-family: Helvetica, Arial, sans-serif;font-size: 13px;color: #545353; }
  47. table {border: none;border-collapse: collapse;}
  48. #sitemap tr:nth-child(odd) td {background-color: #eee !important;}
  49. #sitemap tbody tr:hover td {background-color: #ccc;}
  50. #sitemap tbody tr:hover td, #sitemap tbody tr:hover td a {color: #000;}
  51. #content {margin: 0 auto;width: 1000px;}
  52. .expl {margin: 18px 3px;line-height: 1.2em;}
  53. .expl a {color: #da3114;font-weight: 600;}
  54. .expl a:visited {color: #da3114;}
  55. a {color: #000;text-decoration: none;}
  56. a:visited {color: #777;}
  57. a:hover {text-decoration: underline;}
  58. td {font-size:11px;}
  59. th {text-align:left;padding-right:30px;font-size:11px;}
  60. thead th {border-bottom: 1px solid #000;}
  61. </style>
  62. </head>
  63. <body>
  64. <div id="content">
  65. <h1>XML Sitemap</h1>
  66. <p class="expl">
  67. This is an XML Sitemap generate by <a href="https://namadomain.anda" target="_blank" rel="noopener noreferrer">PHPMU.COM</a>, meant for consumption by search engines. You can find more information about XML sitemaps on <a href="http://sitemaps.org" target="_blank" rel="noopener noreferrer">sitemaps.org</a>.
  68. </p>
  69. <xsl:if test="count(sitemap:sitemapindex/sitemap:sitemap) > 0">
  70. <p class="expl">
  71. This XML Sitemap Index file contains <xsl:value-of select="count(sitemap:sitemapindex/sitemap:sitemap)"/> sitemaps.
  72. </p>
  73. <table id="sitemap" cellpadding="3">
  74. <thead>
  75. <tr>
  76. <th width="75%">Sitemap</th>
  77. <th width="25%">Last Modified</th>
  78. </tr>
  79. </thead>
  80. <tbody>
  81. <xsl:for-each select="sitemap:sitemapindex/sitemap:sitemap">
  82. <xsl:variable name="sitemapURL">
  83. <xsl:value-of select="sitemap:loc"/>
  84. </xsl:variable>
  85. <tr>
  86. <td>
  87. <a href="{$sitemapURL}"><xsl:value-of select="sitemap:loc"/></a>
  88. </td>
  89. <td>
  90. <xsl:value-of select="concat(substring(sitemap:lastmod,0,11),concat(' ', substring(sitemap:lastmod,12,5)),concat(' ', substring(sitemap:lastmod,20,6)))"/>
  91. </td>
  92. </tr>
  93. </xsl:for-each>
  94. </tbody>
  95. </table>
  96. </xsl:if>
  97. <xsl:if test="count(sitemap:sitemapindex/sitemap:sitemap) < 1">
  98. <p class="expl">
  99. This XML Sitemap contains <xsl:value-of select="count(sitemap:urlset/sitemap:url)"/> URLs.
  100. </p>
  101. <table id="sitemap" cellpadding="3">
  102. <thead>
  103. <tr>
  104. <th width="80%">URL</th>
  105. <th width="5%">Images</th>
  106. <th title="Last Modification Time" width="15%">Last Mod.</th>
  107. </tr>
  108. </thead>
  109. <tbody>
  110. <xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/>
  111. <xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
  112. <xsl:for-each select="sitemap:urlset/sitemap:url">
  113. <tr>
  114. <td>
  115. <xsl:variable name="itemURL">
  116. <xsl:value-of select="sitemap:loc"/>
  117. </xsl:variable>
  118. <a href="{$itemURL}">
  119. <xsl:value-of select="sitemap:loc"/>
  120. </a>
  121. </td>
  122. <td>
  123. <xsl:value-of select="count(image:image)"/>
  124. </td>
  125. <td>
  126. <xsl:value-of select="concat(substring(sitemap:lastmod,0,11),concat(' ', substring(sitemap:lastmod,12,5)),concat(' ', substring(sitemap:lastmod,20,6)))"/>
  127. </td>
  128. </tr>
  129. </xsl:for-each>
  130. </tbody>
  131. </table>
  132. </xsl:if>
  133. </div>
  134. </body>
  135. </html>
  136. </xsl:template>
  137. </xsl:output>
  138. </xsl:stylesheet>
  139.  
  140. Oke selesai, simpan semuanya filenya. Setiap kita tambah postingan, maka secara otomatis sitemap ikut terupdate dan setelah itu kakak bisa akses di browser: http://namadomain.anda/sitemap.xml
Donny Indrapati
commented on 27 Apr 2021 09:45:05
  1. Assalamualaikum,.. mas Rizki Amaluddin... ada tutorial lengkap nya...? mohon bantuannya...
Maaf, Untuk Memberikan Komentar Anda Harus Login !!!

Online