Create XML Sitemap with index file in php

Create XML Sitemap with php

Simple and easy methods to create search engine friendly XML Sitemap in php frameworks with index file and a list of sitemaps to submit to Google Search Console

Steps to create a simple XML Sitemap Index file with Sitemaps in MVC Frameworks

  1. Use route file to serve sitemap index and other sitemaps urlset files.
  2. Create a Controller to process data and render the XML view.
  3. Create View files to render the XML Sitemap.

What is a sitemap?

A sitemap is a file where you can list the web pages of your site to tell Google and other search engines about the organisation of your site content. Search engine web crawlers like Googlebot read this file to more intelligently crawl your site.

Also, your sitemap can provide valuable metadata associated with the pages you list in that sitemap: Metadata is information about a webpage, such as when the page was last updated, how often the page is changed, and the importance of the page relative to other URLs in the site.

You can use a sitemap to provide Google with metadata about specific types of content on your pages, including video and image content.

Sitemap extensions for additional media types

Google supports extended sitemap syntax for the following media types. Use these extensions to describe video files, images, and other hard-to-parse content on your site to improve indexing.

For example, you can give Google the information about video and image content.

  • A sitemap video entry can specify the video running time, category, and age appropriateness rating.
  • A sitemap image entry can include the image subject matter, type, and license.

Source: Google


You can provide multiple Sitemap files, but each Sitemap file that you provide must have no more than 50,000 URLs and must be no larger than 50MB (52,428,800 bytes). If you would like, you may compress your Sitemap files using gzip to reduce your bandwidth requirement; however the sitemap file once uncompressed must be no larger than 50MB. If you want to list more than 50,000 URLs, you must create multiple Sitemap files.

The Sitemap index file must:

  • Begin with an opening <sitemapindex> tag and end with a closing </sitemapindex> tag.
  • Include a <sitemap> entry for each Sitemap as a parent XML tag.
  • Include a <loc> child entry for each <sitemap> parent tag.
  • The optional <lastmod> tag is also available for Sitemap index files.

Note: A Sitemap index file can only specify Sitemaps that are found on the same site as the Sitemap index file. For example, can include Sitemaps on but not on or As with Sitemaps, your Sitemap index file must be UTF-8 encoded.

Sample XML Sitemap Index that list two Sitemaps:

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


Sample Code to show how to create Sitemap Index file with Sitemaps in Laravel

Step 1: In app/Http/routes.php (or if you are using Laravel 5.3+ in routes/web.php):

Route::get('/sitemap.xml', 'SitemapController@index');
Route::get('/sitemap/events', 'SitemapController@events');
Route::get('/sitemap/news', 'SitemapController@news');
Route::get('/sitemap/blogs', 'SitemapController@blogs');

Step 2: In SitemapController create function for the index page and other child page or Sitemaps

namespace App\Http\Controllers;
use App\Models\Event;
use Illuminate\Support\Facades\View;
use App\Http\Controllers\Controller;
class SitemapController extends Controller
    public function index()
        $event = Event::orderBy('updated_at', 'desc')->first();
        $event['url'] = url('/sitemap/events');
        return response()->view('sitemap.index', ['event' => $event, ])->header('Content-Type', 'text/xml');
    public function events()
        $events = Event::orderBy('updated_at', 'desc')->get();

        return response()
            ->view('', ['events' => $events, ])->header('Content-Type', 'text/xml');

Step 3: Create View for Sitemap Index and Sitemap files

In this example we will show View file for Index file and Sitemap for events

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<sitemapindex xmlns="">
    <lastmod>{{ $event->updated_at->tz('UTC')->toAtomString() }}</lastmod>

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset xmlns="">
  @foreach ($events as $event)

That's all, you are all set to go with your dynamically generated sitemap.

Submitting XML Sitemap to Google, bing and other Search Engines

So, we have completed creating our dynamically generated sitemap, now next step is to submit to Google Webmaster Console or Bing.

  • Sign in to your Google Search Console, and go to you added property.
  • In dropdown Crawl, section Sitemaps, you can find the options to add/test your Sitemap.
  • Add sitemap.xml which you have created in previous step. If there is no errors, you are all set to go.

Good Luck.

Recent resources