How to Solve CORS error in accessing laravel routes

How to Solve CORS error in accessing laravel routes

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Solve CORS error in accessing laravel routes

When building an Outlook web addon that communicates with a Laravel backend, the browser's Same-Origin Policy will aggressively block requests if your server doesn't explicitly permit them. The error you are seeing—No 'Access-Control-Allow-Origin' header is present on the requested resource—indicates that the browser received a response from https://test.com but lacked the necessary headers to allow the request originating from https://localhost:44377.

The most critical issue in your current setup is the mismatch between the protocol used by the client and the server. You noted that requests are automatically redirecting from HTTP to HTTPS, yet your browser is trying to hit an HTTPS endpoint. If your local development environment or your addon's manifest points to http://test.com, but the server has forced a redirect to https://test.com, you are creating a protocol mismatch before the CORS logic even gets a chance to run.

To resolve this, you must ensure that the URL scheme (http vs https) matches exactly on both the client side and the server side. If your Outlook addon is configured to call http://test.com/api/test, your Laravel application should be accessible via HTTP, or you must configure your web server (Nginx or Apache) to handle the redirect logic consistently without breaking the origin check.

Once the protocol mismatch is resolved, you need to implement a robust CORS strategy within Laravel. Simply adding raw headers in bootstrap/app.php using PHP's header() function is unreliable for modern Laravel applications because these headers are often stripped or ignored depending on how the request enters the application (e.g., via Nginx proxying). Instead, you should use Laravel's dedicated middleware to handle these requirements.

Install the spatie/laravel-cors package using Composer:

composer require spatie/laravel-cors

Next, publish the configuration file:

php artisan vendor:publish --tag="cors"

Open the published config file at config/cors.php. You need to define which origins are allowed. For development and local testing, you can be permissive, but in production, you should restrict this to specific domains.

return [
    'paths' => ['api/*', 'sanctum/csrf-cookie'],
    
    'allowed_methods' => ['*'],
    'allowed_origins' => ['*'], // Use a specific origin like 'https://localhost:44377' in production
    'allowed_origins_patterns' => [],
    'allowed_headers' => ['*'],
    'exposed_headers' => [],
    'max_age' => 0,
    'supports_credentials' => false, // Set to true only if you need cookies/auth headers
];

After updating the configuration, apply the middleware to your application's route group or globally via Kernel.php (or the app bootstrap in Laravel 11).

Route::middleware(\Spatie\Cors\Middleware\HandleCors::class)->group(function () {
    Route::get('/api/test', function () {
        return 'Hello World';
    });
});

Regarding your specific error regarding the redirect, ensure that your .env file and Nginx/Apache configuration are aligned. If you are running Laravel in a Docker container, check your docker-compose.yml to ensure port 443 (HTTPS) is not being forced when it shouldn't be. The APP_URL environment variable should match the scheme you intend to use locally.

A common pitfall with Outlook web addons is that they often run in an iframe or a specific sandboxed environment. Ensure that your addon's manifest (manifest.json) correctly defines the endpoint URL. If the manifest says http://test.com but the server redirects to https://test.com, the browser will see the request origin as https://test.com (after redirect) and might still block it if the server headers do not explicitly allow that specific origin.

By configuring spatie/laravel-cors correctly and ensuring your server's URL scheme matches your client's expectations, you will eliminate the CORS policy violation. Remember that allowing all origins (*) is acceptable for development but poses a security risk in production; always tighten the allowed_origins list once you move to a live environment.

Our network