The upgrade-insecure-requests Directive
What you'll learn: How to use the upgrade-insecure-requests CSP directive to automatically upgrade all HTTP requests to HTTPS, preventing mixed content issues.
What Is Mixed Content?
When a page loads over HTTPS but requests resources (images, scripts, stylesheets) over HTTP, browsers flag this as "mixed content." It's like having a secure lock on your front door but leaving windows wide open—the security chain is only as strong as its weakest link. Browsers often block these insecure requests, breaking your site's functionality or appearance.
The upgrade-insecure-requests Solution
The upgrade-insecure-requests directive tells the browser: "Whenever this page tries to fetch something over HTTP, silently upgrade it to HTTPS instead." This prevents mixed content warnings without requiring you to manually update every URL in your codebase.
How to Use It
Add this directive to your CSP header:
Content-Security-Policy: upgrade-insecure-requests;
Or via meta tag:
<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
Now if your page contains:
<img src="http://example.com/photo.jpg">
The browser automatically requests https://example.com/photo.jpg instead.
When It's Useful
This directive is particularly valuable when:
- Migrating from HTTP to HTTPS and legacy content references old URLs
- Embedding third-party content that might use HTTP links
- Ensuring all subresources load securely without hunting down every reference
Note that the target server must support HTTPS, or the request will fail. This directive doesn't magically make insecure servers secure—it just upgrades the protocol used to contact them.
Key Takeaway: The upgrade-insecure-requests directive automatically converts HTTP resource requests to HTTPS, eliminating mixed content issues during HTTPS migrations and ensuring consistent secure connections.