nexxai's avatar
Level 37

Sitemap.xml 404 - can't understand why

I'm using Spatie's laravel-sitemap package, and while I can generate the sitemap no problem (when I look at the actual filesystem, it's right there in storage/app/public with the correct content), but when I actually try to visit https://mysite.com/sitemap.xml, it gives me a 404.

I've done the php artisan storage:link and there's nothing in the laravel.log file that would suggest anything is configured wrong, so I'm kind of at a loss for what might be the problem.

Anyone have any suggestions?

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

It sounds like you've generated the sitemap and stored it in the storage/app/public directory, which is a good first step. However, if you're getting a 404 error when trying to access https://mysite.com/sitemap.xml, it's likely that the web server is not correctly serving the file from the public directory or the symbolic link from public/storage to storage/app/public is not correctly set up.

Here are a few steps you can take to troubleshoot and resolve the issue:

  1. Verify the Symbolic Link: Make sure that the symbolic link from public/storage to storage/app/public is correctly set up. You can do this by running the following command in your project root:

    ls -la public/
    

    You should see an entry for storage that points to ../storage/app/public. If it's not there, you can create it by running:

    php artisan storage:link
    
  2. Check the .htaccess File: If you're using Apache, ensure that the .htaccess file in your public directory is correctly configured to allow access to the storage directory. The default Laravel .htaccess should work fine, but if you've made custom changes, it could be causing issues.

  3. Web Server Configuration: If you're using Nginx or a different web server, make sure that your server configuration is set up to serve files from the public/storage directory. For Nginx, you might need to add a location block to your site configuration:

    location /storage {
        alias /path/to/your/project/storage/app/public;
        try_files $uri $uri/ =404;
    }
    
  4. Move the Sitemap to the Public Directory: As an alternative, you could move the sitemap file to the public directory. This way, it will be directly accessible without the need for a symbolic link. You can modify the sitemap generation to place the file in the public directory:

    SitemapGenerator::create(config('app.url'))
        ->writeToFile(public_path('sitemap.xml'));
    

    After doing this, you should be able to access the sitemap at https://mysite.com/sitemap.xml.

  5. Check File Permissions: Ensure that the sitemap.xml file has the correct permissions to be readable by the web server. You can set the permissions using:

    chmod 644 storage/app/public/sitemap.xml
    
  6. Clear Cache: Sometimes, routes and configurations might be cached. Clear your application cache by running:

    php artisan cache:clear
    
  7. Check Routes: If you have a route defined for sitemap.xml, make sure it's correctly returning the sitemap file. If not, you can define one in your routes/web.php:

    Route::get('/sitemap.xml', function () {
        return response()->file(public_path('storage/sitemap.xml'), [
            'Content-Type' => 'application/xml'
        ]);
    });
    

Try these steps and see if any of them resolve the issue. If you're still experiencing problems, you may need to provide more information about your server configuration for further assistance.

nexxai's avatar
Level 37

I ended up using Lary's step 7 suggestion and just added a manual route to it, and it works now. I was hoping to not have to do this (it feels "hacky") but it works, so I'm going to leave it for now.

Please or to participate in this conversation.