Search code examples
phplaravelroutessitemap

Unable to add sitemap.xml file and route in laravel 9


I am trying to add sitemap.xml file for an laravel website. I am using laravel version 9. I tried to search on google and youtube but not able to find any solution.

I have created a sitemap.xml file and I have moved sitemap.xml file to the public directory and I have added the route in the routes folder/directory like this to the file web.php where other routes are defined:

Route::get("sitemap.xml" , function () {
return \Illuminate\Support\Facades\Redirect::to('sitemap.xml');
 });

I have also tried to added the following code to the routes folder/directory like this :

Route::get('sitemap.xml',function() {
return response()->view('sitemap')
->header('Content-Type', 'xml');
});

Now open the xml file through url: https://www.example.com/sitemap.xml, I get 500 SERVER ERROR.


Solution

  • You cannot link to a file, with a view method call. Put the file in the storage folder, and then use the file call to actually serve the file.

    return response()->file('sitemap.xml');