enable cors in .htaccess

enable cors in .htaccess

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

Stefan Bogdanescu

Founder & Senior Architect

Enabling CORS in .htaccess: A Deep Dive into XSLD and Pre-Flight Requests

Configuring Cross-Origin Resource Sharing (CORS) is a frequent requirement when bridging frontend applications, like Angular.js projects, with backend services running on different domains. While the Access-Control-Allow-Origin header seems straightforward to add via .htaccess, relying solely on it often leads to silent failures or specific browser errors, such as the XHR status 0 error seen during pre-flight checks.

In Apache environments using mod_rewrite and mod_headers, enabling CORS requires more than just setting a single header. The most common pitfall occurs when the server responds with a standard HTTP status code (like 200 OK) for an OPTIONS request instead of the required 204 No Content. Modern browsers treat this as a failure to meet CORS policy requirements, resulting in the console errors described in typical debugging logs where the XHR event sequence ends abruptly with an error state.

The Critical Role of OPTIONS Requests

When a browser encounters a cross-origin request that uses credentials or includes non-simple headers (like Content-Type with application/json), it sends a pre-flight OPTIONS request before the actual data is transmitted. If your .htaccess configuration does not explicitly handle this method, Apache might default to returning a 405 Method Not Allowed or, worse, a 200 response without the necessary headers for that specific method.

To resolve this, you must ensure that OPTIONS requests are intercepted and responded to correctly. In your .htaccess file, you should explicitly define the behavior for these requests before the generic rewrite rules take over. A robust configuration looks like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /index.php [QSA,L]

# Handle CORS Headers
<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Methods "GET, POST, OPTIONS, DELETE, PUT"
    Header set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept, Authorization"
</IfModule>

# Explicitly handle OPTIONS requests
RewriteCond %{REQUEST_METHOD} ^OPTIONS
RewriteRule ^ - [R=204,L]

This snippet ensures that when an OPTIONS request arrives, the server returns a 204 status code immediately without processing further rules. The Header set Access-Control-Allow-Headers directive is also crucial; if your Angular app sends custom headers like Authorization, the server must acknowledge them or the browser will block the request.

Security Implications and Best Practices

Setting Access-Control-Allow-Origin to * is convenient for development but poses significant security risks in production, especially if your API handles sensitive data or accepts credentials (cookies). In a production environment, it is best practice to replace the wildcard with specific allowed origins:

Header set Access-Control-Allow-Origin "https://your-angular-app.com"

This restricts access to only the trusted domain. You should also consider setting Access-Control-Max-Age to cache the pre-flight response for a reasonable duration, reducing the load on your server and improving frontend performance:

Header set Access-Control-Max-Age 86400

Alternatives to .htaccess

While .htaccess is convenient, it introduces overhead because Apache processes these directives at every request. If you are running a high-traffic API or need more complex logic, consider moving the CORS configuration into your application code. For PHP frameworks like Slim or Laravel, setting headers directly in the controller or middleware is often cleaner and avoids the file-system latency associated with .htaccess lookups.

For instance, in a PHP route handler, you can set these headers dynamically based on the incoming request origin:

header('Access-Control-Allow-Origin: ' . $request->getHeaderLine('Origin'));
header('Access-Control-Allow-Methods: GET, POST, OPTIONS, DELETE, PUT');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization');

This approach gives you finer control and eliminates the need to maintain separate configuration files. If you are working with a Laravel-based backend, similar middleware can be registered in app/Http/Middleware to handle these headers globally before the request reaches your controllers.

Ultimately, while adding a single line to .htaccess might seem sufficient for simple setups, the reality of browser security policies demands a more rigorous approach to handling OPTIONS requests and managing header permissions.

Our network