Laravel Sitemap is a robust and intelligent solution for automatically generating XML sitemaps for your Laravel application. Built on top of Spatie's excellent sitemap package, it provides advanced crawling capabilities, model integration, and flexible configuration options.
- PHP ^8.3
- Laravel ^10.0 || ^11.0 || ^12.0
- Spatie Sitemap ^2.0
- Spatie Crawler ^7.0
Install the package via Composer:
composer require fuelviews/laravel-sitemap
Publish the configuration file:
php artisan vendor:publish --tag="sitemap-config"
This will create a config/fv-sitemap.php
file where you can customize your sitemap settings.
Generate your sitemap using the Artisan command:
php artisan sitemap:generate
Once generated, your sitemap will be available at:
https://yoursite.com/sitemap.xml
use Fuelviews\Sitemap\Facades\Sitemap;
// Get sitemap content (generates if not exists)
$content = Sitemap::getSitemapContents('sitemap.xml');
Link to your sitemap in templates:
<link rel="sitemap" type="application/xml" title="Sitemap" href="{{ route('sitemap') }}" />
The main configuration options in config/fv-sitemap.php
:
return [
// Storage disk for sitemap files
'disk' => env('SITEMAP_DISK', 'public'),
// Generate sitemap index for large sites
'exclude_subcategory_sitemap_links' => env('SITEMAP_USE_INDEX', true),
// Exclude redirect URLs
'exclude_redirects' => env('SITEMAP_EXCLUDE_REDIRECTS', true),
// Routes to exclude
'exclude_route_names' => [
// 'admin.*',
// 'api.*',
],
// Paths to exclude
'exclude_paths' => [
// '/admin',
// '/dashboard',
],
// Specific URLs to exclude
'exclude_urls' => [
'/sitemap.xml',
'/pages_sitemap.xml',
'/posts_sitemap.xml',
],
// Models to include in posts sitemap
'post_model' => [
// App\Models\Post::class,
],
];
You can use environment variables for common settings:
# .env file
SITEMAP_DISK=public
SITEMAP_USE_INDEX=true
SITEMAP_EXCLUDE_REDIRECTS=true
To include your Eloquent models in the sitemap, implement the Sitemapable
interface:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Spatie\Sitemap\Contracts\Sitemapable;
use Spatie\Sitemap\Tags\Url;
class Post extends Model implements Sitemapable
{
/**
* Convert the model instance into a sitemap URL entry.
*/
public function toSitemapUrl(): Url
{
return Url::create(route('posts.show', $this))
->setLastModificationDate($this->updated_at)
->setChangeFrequency('weekly')
->setPriority(0.8);
}
}
Then add your model to the configuration:
// config/fv-sitemap.php
'post_model' => [
App\Models\Post::class,
App\Models\Article::class,
],
When exclude_subcategory_sitemap_links
is false
, generates one sitemap containing all content:
- All crawlable pages
- All configured model entries
When exclude_subcategory_sitemap_links
is true
, generates:
sitemap.xml
- Sitemap index filepages_sitemap.xml
- All crawled pagesposts_sitemap.xml
- All model entries
Exclude specific Laravel routes:
'exclude_route_names' => [
'admin.dashboard',
'api.users.index',
'auth.*', // Wildcard support
],
Exclude URL paths (supports prefixes):
'exclude_paths' => [
'/admin', // Excludes /admin, /admin/users, etc.
'/dashboard', // Excludes /dashboard and sub-paths
'/api', // Excludes all API endpoints
],
Exclude specific URLs:
'exclude_urls' => [
'/login',
'/register',
'/sitemap.xml', // Don't include sitemap in itself
],
Use different storage disks:
// config/fv-sitemap.php
'disk' => 's3', // Store sitemaps on S3
// Or use environment variable
'disk' => env('SITEMAP_DISK', 'public'),
The package automatically generates sitemaps when they're requested but don't exist. This means:
- A user visits
/sitemap.xml
- If the sitemap doesn't exist, it's generated on-the-fly
- The generated sitemap is served immediately
- Future requests serve the cached version
For sites with many pages, use sitemap indexes:
'exclude_subcategory_sitemap_links' => true,
This creates separate sitemaps for pages and posts, improving crawl efficiency.
The package uses Spatie's crawler with optimized settings:
- Ignores robots.txt for complete site mapping
- Filters out query parameters
- Normalizes URLs to prevent duplicates
- Excludes redirect URLs by default
- Generated sitemaps are stored on your configured disk
- HTTP responses include appropriate cache headers
- Sitemaps are only regenerated when missing
php artisan sitemap:generate
Generates the complete sitemap structure based on your configuration.
php artisan vendor:publish --tag="sitemap-config"
Publishes the configuration file to config/fv-sitemap.php
.
Run the package tests:
composer test
Run tests with coverage:
composer test-coverage
- Check configuration: Ensure
config/fv-sitemap.php
is properly configured - Model issues: Verify models implement
Sitemapable
interface - Storage permissions: Ensure the configured disk is writable
- Route accessibility: Verify your application routes are accessible
- Exclusion rules: Check if your exclusion rules are too broad
- Route registration: Ensure your routes are properly registered
- Model queries: Verify your models have
published
status records
- Use sitemap indexes: Enable
exclude_subcategory_sitemap_links
- Limit model queries: Add additional constraints in your models
- Increase memory limit: Adjust PHP memory limit for generation
Enable debug logging by checking Laravel logs during generation:
tail -f storage/logs/laravel.log
The package logs detailed information about:
- Generation process
- Excluded URLs and reasons
- Model processing
- Storage operations
- Use clean URLs without query parameters
- Implement proper canonical URLs
- Ensure consistent URL structure
Configure appropriate change frequencies in your models:
public function toSitemapUrl(): Url
{
return Url::create(route('posts.show', $this))
->setLastModificationDate($this->updated_at)
->setChangeFrequency('weekly') // daily, weekly, monthly
->setPriority(0.8); // 0.0 to 1.0
}
Register your sitemap with search engines:
- Google Search Console
- Bing Webmaster Tools
- Add
<link rel="sitemap">
to your HTML head
Please see CONTRIBUTING for details.
Please see CHANGELOG for more information on what has changed recently.
Please review our security policy on how to report security vulnerabilities.
- Joshua Mitchener - Lead Developer
- Daniel Clark - Contributor
- Fuelviews - Organization
- Spatie - Underlying sitemap and crawler packages
- All Contributors
The MIT License (MIT). Please see License File for more information.
Built with β€οΈ by the Fuelviews team