How to Create an XML Sitemap | Register a Sitemap with Google
Creating an XML Sitemap and Registering it with Google
An XML sitemap is a file that lists all the important pages of a website, making it easier for search engines like Google to find and crawl them. This article will guide you through the process of creating an XML sitemap and registering it with Google.
What is an XML Sitemap?
An XML sitemap is a file that lists the URLs of a website in a structured format. It helps search engines understand the website structure and find all the important pages, even if the internal linking is not perfect. XML sitemaps can be beneficial for SEO, as they allow Google to quickly find and index the essential pages of a website.
How to Create an XML Sitemap
Creating an XML sitemap can be done manually or with the help of a plugin or tool. Here, we will discuss how to create an XML sitemap using PHP.
First, you need to create a new file called "sitemap-index.php" and add the following code:
<?php
namespace ProcessWire;
$out = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
$templates = "basic-page|blog-post|tag";
$key = $pages->count("template=$templates");
$limit = 200;
$pageNum = ceil($key/ $limit);
$post = $pages->get("template=sitemap-xml");
$i = 1;
while($pageNum >= $i){
$out .= "\n<sitemap>" .
"\n\t<loc>" . $post->httpUrl . "page$i/</loc>" .
"\n\t<lastmod>" . date("Y-m-d", $post->modified) . "</lastmod>" .
"\n</sitemap>";
$i = $i + 1;
}
$out .= "\n</sitemapindex>";
header("Content-Type: text/xml");
echo $out;
?>
This code will create a sitemap index file that lists all the sitemap files for your website. Each sitemap file will contain up to 200 URLs.
Next, you need to create a new file called "sitemap-xml.php" and add the following code:
<?php
namespace ProcessWire;
$out = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
$pageArray = $pages->find("template=basic-page|blog-post|tag, limit=200");
foreach ($pageArray as $post) {
$out .= "\n<url>" .
"\n\t<loc>" . $post->httpUrl . "</loc>" .
"\n\t<lastmod>" . date("Y-m-d", $post->modified) . "</lastmod>" .
"\n</url>";
}
$out .= "\n</urlset>";
header("Content-Type: text/xml");
echo $out;
?>
This code will create a sitemap file that lists up to 200 URLs of your website.
How to Register a Sitemap with Google
Once you have created your XML sitemap, you need to register it with Google. Here are the steps to do so:
- Sign in to Google Search Console.
- Select your website from the list.
- Click on "Sitemaps" in the left-hand menu.
- Enter the URL of your sitemap in the "Add a new sitemap" field.
- Click "Submit."
Google will now crawl your sitemap and index the URLs listed in it. You can check the status of your sitemap in the Google Search Console.
Conclusion
Creating an XML sitemap and registering it with Google is an essential step