How to make a sitemap using PHP script - somelinesforyou

How to make a sitemap using PHP script

  • By- Admin
  • On- 27 Dec, 2019
  • Tags: Technical

How to make a sitemap using PHP script?

For coding metric XML sitemap generation. You need to follow some structure to generate it by yourself.

  1. Understand your site URLs structure.
  2. Understand the coding structure for URLs.
  3. Understand the priority of each page.
  4. Validate the .XML sitemap code.
  5. How to submit your sitemap to Google

 

Step 1: Understand your site URLs structure.

Learn all things about your sites' URLs flows such as the main header has a home/, contact/, about/, topic/, blog/ etc.

1577804913_chart-min.jpg

If you note blog/  particular URL, so obtusely there some particular blog categories will available.

Those all blog categories will come from a blog-category database. So here you need to understand URLs structure for blog.

That will be something looking like this "https://example.com/blog/blog-category-name" for all particular blogs' categories.

 

Step 2: Understand the coding structure for URLs.

So, here how can create an XML sitemap for blogs' URL.

Database details: table_name "blog_category", table filed for blogs' category is "blog_cat_name".

Now we need to write code that will give.XML as output with blogs' category sitemap URLs.

Example code:

<?php

// this file name is sitemap_generator.php

// file path is, you root project folder, example (https://example.com/sitemap_generator.php)

// HERE ALL ECHO ARE FOR PRINT XML FORMATE VIEW INTO SCREEN. NOTHING MORE. $xmlString VARIBLE IS CREATING XML FILE AND STORE INTO YOUR GIVEN PATH

$connect = mysqli_connect("localhost", "user_name", "password", "database_name");

// set base_url

$base_url = "https://example.com/";

// header for xml

header("Content-Type: application/xml; charset=utf-8");

// start to build our XML file structure

echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; 

echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . PHP_EOL;

// xmlString  varible for genrate XML file.

$xmlString = '';

$xmlString .= '<?xml version="1.0" encoding="UTF-8"?>';

$xmlString .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';

$query = "SELECT `blog_cat_name` FROM `blog_category` ";

$result = mysqli_query($connect, $query);

while($row = mysqli_fetch_array($result)) {

    $data = str_replace(' ', '-',$row["blog_cat_name"]);

    echo '<url>' . PHP_EOL;

    echo '<loc>'.$base_url.'blog/'.strtolower($data).'</loc>' . PHP_EOL;

    //  mktime(hour, minute, second, month, day, year, is_dst)

    echo '<lastmod>'.date(DATE_ATOM,mktime(3,45,26,12,25,2019)).'</lastmod>' . PHP_EOL;

    echo '<priority>0.80</priority>' . PHP_EOL;

    echo '</url>' . PHP_EOL;

    $xmlString .= '<url>';

    $xmlString .= '<loc>'.$base_url.'category/'.strtolower($data).'</loc>';

    $xmlString .= '<lastmod>'.date(DATE_ATOM,mktime(3,45,26,12,25,2019)).'</lastmod>';

    $xmlString .= '<priority>0.80</priority>';

    $xmlString .= '</url>';

}

echo '</urlset>' . PHP_EOL;

$xmlString .= '</urlset>';

$dom = new DOMDocument;

$dom->preserveWhiteSpace = FALSE;

$dom->loadXML($xmlString);

$dom->formatOutput = TRUE;

echo $dom->saveXml();

$dom->save("sitemap.xml");

// it means my sitemap.xml file is store into our main root folder near the out sitemap_generator.php

?>

 

Here, you can put any XML tags as per your requirements.

You can follow this code structure for all your particular URLs and sub URLs.

Such as https://example.com/blog/music/top-10-best-music-album, and many more.

 

Step 3: Understand the priority of each page.

is the only way to understand which URLs are more effective than the other URLs.

For example

  • Home page priority " 1 ", it means, it is the main page.
  • Category page priority may be "0.80", which means, it is also an important page but not the home page.
  • A particular blog post may be "0.64", which means, it is also an important page but not 0.64 above priorities pages.

 

Recommended XML sitemap priority list.

  1. 0.8 - 0.1: Home page, product information, landing pages.
  2. 0.4 - 0.7: Articles, blog posts, FAQs, etc.
  3. 0.0 - 0.3: Outdated data or old news and information that has become less relevant.

 

Step 4: Validate the.XML sitemap code.

After Complete all things and you have your own generated XML sitemap file. Before setting it on Google search console you need to check it, your sitemap.xml file did becomes proper or not.

Fortunately, there are many tools that will help validate your sitemap.xml is correct or not. There’s software available online that can help you do this.

My recommended is Validate XML sitemap

1577439674_sitemap-xml-check.png

If your XML file has any error, it can help to find the best way to solve it.

 

Step 5: How to submit your sitemap to Google.

It's pretty easy to submit on Google search console.

First, your proper sitemap.xml file upload on your project's main root directory.

such as https://exapmle.com/sitemap.xml

Then go to the google search console. Add Property as per your choices such as Domai or URLs proxy.

You can see this link for detailed understanding

Leave your comment

Your comment has been sent. Thank you!
We'll never share your email with anyone else.