REGEXVAULTv2.0
Web & Network/HTTP
Verified Safe

Cache-Control Directive Regex for PHP

/^(?:no-cache|no-store|no-transform|public|private|must-revalidate|proxy-revalidate|only-if-cached|immutable|max-age=[0-9]{1,10}|s-maxage=[0-9]{1,10}|max-stale(?:=[0-9]{1,10})?|min-fresh=[0-9]{1,10}|stale-while-revalidate=[0-9]{1,10}|stale-if-error=[0-9]{1,10})(?:\s*,\s*(?:no-cache|no-store|no-transform|public|private|must-revalidate|proxy-revalidate|only-if-cached|immutable|max-age=[0-9]{1,10}|s-maxage=[0-9]{1,10}|max-stale(?:=[0-9]{1,10})?|min-fresh=[0-9]{1,10}|stale-while-revalidate=[0-9]{1,10}|stale-if-error=[0-9]{1,10}))*$/i

What this pattern does

This page provides a comprehensive, battle-tested regular expression for matching cache-control directive, ported and verified for PHP. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your PHP project — whether you're validating in a Laravel validator, a WordPress plugin, or a standalone PHP script.

Php Implementation

Php
<?php
// Cache-Control Directive
// ReDoS-safe | RegexVault — Web & Network > HTTP

define('CACHECONTROL_DIRECTIVE_PATTERN', '/^(?:no-cache|no-store|no-transform|public|private|must-revalidate|proxy-revalidate|only-if-cached|immutable|max-age=[0-9]{1,10}|s-maxage=[0-9]{1,10}|max-stale(?:=[0-9]{1,10})?|min-fresh=[0-9]{1,10}|stale-while-revalidate=[0-9]{1,10}|stale-if-error=[0-9]{1,10})(?:\s*,\s*(?:no-cache|no-store|no-transform|public|private|must-revalidate|proxy-revalidate|only-if-cached|immutable|max-age=[0-9]{1,10}|s-maxage=[0-9]{1,10}|max-stale(?:=[0-9]{1,10})?|min-fresh=[0-9]{1,10}|stale-while-revalidate=[0-9]{1,10}|stale-if-error=[0-9]{1,10}))*$/');

function validate_cachecontrol_directive(string $input): bool {
    return (bool) preg_match(CACHECONTROL_DIRECTIVE_PATTERN, $input);
}

// Example
var_dump(validate_cachecontrol_directive("no-cache")); // bool(true)

Test Cases

Matches (Valid)
Rejects (Invalid)
no-cachemax-age=abc
max-age=3600invalid-directive
public, max-age=86400cache-control
no-store, no-cachemax-age=-1
max-age=0, must-revalidatemax-age=

When to use this pattern

This pattern is drawn from the Web & Network > HTTP category and carries a ReDoS-safe certification. That matters for PHP developers because especially relevant in PHP where PCRE backtracking limits can trigger silent failures on malicious input. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.

Common Pitfalls

Cache-Control directives are space-tolerant after commas but must not have spaces around = signs. The pattern enforces no spaces around =.

Technical Notes

max-age values are in seconds. max-age=0 combined with must-revalidate is equivalent to no-cache for strict clients. Stale-while-revalidate is from RFC 5861.

Have a pattern that belongs in the vault?

Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.

Submit a Pattern