I am using spatie to generate sitemap.xml. It is working properly as it is expected. But as per some requirement I would like to not include any image url in sitemap.xml.
This is my code –
public function sitemap(){ $link = Website::where('adminId', Auth::user()->id)->value('link'); $path = public_path('uploads/'.Auth::user()->id.'/sitemap.xml'); if (file_exists($path)) { File::delete($path); fopen($path, 'w'); } else { fopen($path, 'w'); } SitemapGenerator::create($link)->writeToFile($path); return response()->json('success'); }
Currently my sitemap.xml
something looks like this with image links –
<url> <loc>http://www.example.com/uploads/3/page/anahera/1591769129-4412.jpeg</loc> <lastmod>2020-06-23T11:22:59+12:00</lastmod> <changefreq>daily</changefreq> <priority>0.8</priority> </url> <url> <loc>http://www.example.com/uploads/3/page/anahera/1591769136-7646.jpeg</loc> <lastmod>2020-06-23T11:22:59+12:00</lastmod> <changefreq>daily</changefreq> <priority>0.8</priority> </url>
This there any way to remove those image links from sitemap.xml?
Appreciate for help.
Advertisement
Answer
Please follow this part of document – spatie. This is not specifically omit images but if there is any specific format in your url for images link – http://www.example.com/uploads
then try to get segment(1)
and compare so this part of documentation will work.
In your case as an example –
public function sitemap(){ $link = Website::where('adminId', Auth::user()->id)->value('link'); $path = public_path('uploads/'.Auth::user()->id.'/sitemap.xml'); if (file_exists($path)) { File::delete($path); fopen($path, 'w'); } else { fopen($path, 'w'); } SitemapGenerator::create($link)->hasCrawled(function (Url $url) { if ($url->segment(1) === 'uploads') { return; } return $url; })->writeToFile($path); return response()->json('success'); }
Hope this will work for you.