Home arrow PHP arrow Using PHP to Generate a Customized Google XML Sitemap

Using PHP to Generate a Customized Google XML Sitemap

Submitting XML sitemaps to Google Webmaster Tools is an important webmaster activity. Google recommends using it to help it find new content in your website which otherwise cannot be crawled by Googlebot. This is particularly helpful if you have a fairly large website. In this article, you'll learn how to generate such a sitemap with PHP.

TABLE OF CONTENTS:
  1. Using PHP to Generate a Customized Google XML Sitemap
  2. The Web Form's HTML Code
  3. The PHP Server Side Script
  4. Implementation Guide
By: Codex-M
Rating: starstarstarstarstar / 4
June 16, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

If you are new to XML sitemaps or need more information about Google XML sitemap generation, you should read this tutorial before going any further.

This tutorial helps you develop your own PHP-based Google XML Sitemap generator. This is a customized approach, which means that you can feed the application your canonical URLs (sorted by highest to lowest in terms of importance and priority) and it will output the XML syntax for your sitemap.xml.

The XML syntax can then be copy to a blank text file (for example, in notepad) and uploaded to your website.

This offers a lot of advantages compared to other XML sitemap solutions available online, for two reasons. First, Google advises webmasters to submit XML sitemaps that contain ONLY canonical URLs. If you use third party sitemap generator software that you can find online, you will need to do a lot of work filtering the sitemap's XML code for canonical URLs.

Second, other sitemap generators impose a limit. For example, they may only process 500 URLs. This can strongly affect you if your website has more than 500 canonical URLs.

Web Application Design of Customized Sitemap Generator

Any PHP web application starts with a design.  The canonical URLs are put into a web form. Since there is more than one line of input (the typical number of canonical URLs for a large website can exceed 100), you are going to use a text area instead of a single text box, so that all URLs can be accommodated.

Screen shot:

 

The actual PHP script will then be used to perform four tasks:

1. Parse the URL from the web form.

2. Compute the priorities of the URL, which is used for the sitemap.

3. Generate the XML syntax.

4. Output the generated syntax with computed priorities back to the user's web browser.

The XML syntax should conform to the Google XML sitemap standard. An example of a conforming XML syntax is:

<?xml version="1.0" encoding="UTF-8"?>

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

 

<url>

  <loc>http://www.php-developer.org/ </loc>

  <priority>1</priority>

</url>

 

<url>

  <loc>http://www.php-developer.org/how-long-does-it-take-for-dns-to-propagate-in-godaddy-hosting/ </loc>

  <priority>0.97</priority>

</url>

 

<url>

  <loc>http://www.php-developer.org/solution-for-blogger-you-are-about-to-redirected-message-problem/ </loc>

  <priority>0.95</priority>

</url>

</urlset>

To easily troubleshoot XML syntax, it is best to use standard colors for the XML syntax. You can easily distinguish the URLs from the XML tags:

Of course, since you are accepting user inputs, you might as well filter bots from using the web form with a security code or a captcha system.



 
 
>>> More PHP Articles          >>> More By Codex-M
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap

Dev Shed Tutorial Topics: