Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dinni's avatar
Level 1

Sitemap for multiple domain

hi i have 2 domains which is pointed to same project in the server Now i have 2 sitemaps, how i can put both in public folder?

i want it like

xyz.com/sitemap.xml abc.com/sitemap.xml

how i can do this?

0 likes
3 replies
LaryAI's avatar
Level 58

To achieve this, you can create two separate sitemap files for each domain and place them in the public folder of your Laravel project. Then, you can create two routes in your web.php file to serve the sitemap files for each domain. Here's an example:

  1. Create two sitemap files in the public folder:
public/sitemap-xyz.xml
public/sitemap-abc.xml
  1. Create two routes in your web.php file:
Route::get('sitemap.xml', function () {
    return response()->file(public_path('sitemap-xyz.xml'));
})->name('sitemap.xyz');

Route::domain('abc.com')->get('sitemap.xml', function () {
    return response()->file(public_path('sitemap-abc.xml'));
})->name('sitemap.abc');
  1. In your robots.txt file, add the following lines to point to the sitemap files:
Sitemap: https://xyz.com/sitemap.xml
Sitemap: https://abc.com/sitemap.xml

With these changes, you should be able to access the sitemap files for each domain at the following URLs:

https://xyz.com/sitemap.xml
https://abc.com/sitemap.xml
gych's avatar

@Saddamhossain You could try to add the sitemap location to robots.txt but this also needs to be dynamically generated based on the current domain.

Here is another approach you could use

sitemap.xml

Route::get('/sitemap.xml', function () {
    $host = request()->getHttpHost();

    $sitemapPath = '';
    switch ($host) {
        case 'xyz.com':
            $sitemapPath = public_path('sitemap-xyz.xml');
            break;
        case 'abc.com':
            $sitemapPath = public_path('sitemap-abc.xml');
            break;
        default:
            abort(404); 
    }

    return response()->file($sitemapPath, ['Content-Type' => 'application/xml']);
});

robots.txt

Route::get('/robots.txt', function () {
    $host = request()->getHttpHost();

    $robotsContent = "User-agent: *\nDisallow:\n";
    switch ($host) {
        case 'xyz.com':
            $robotsContent .= "Sitemap: https://xyz.com/sitemap.xml";
            break;
        case 'abc.com':
            $robotsContent .= "Sitemap: https://abc.com/sitemap.xml";
            break;
    }

    return response($robotsContent, 200, ['Content-Type' => 'text/plain']);
});

Please or to participate in this conversation.