Apache/Nginx Combined Log Format Regex for PHP
/^(\S+) \S+ (\S+) \[([^\]]+)\] "([A-Z]+) ([^"]+) HTTP/[\d.]+" ([1-5]\d{2}) (\d+|-)(?:\s"([^"]*)" "([^"]*)")?/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching apache/nginx combined log format, 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
// Apache/Nginx Combined Log Format
// ReDoS-safe | RegexVault — Dev & Systems > Log Parsing
define('APACHENGINX_COMBINED_LOG_FORMAT_PATTERN', '/^(\S+) \S+ (\S+) \[([^\]]+)\] "([A-Z]+) ([^"]+) HTTP\/[\d.]+" ([1-5]\d{2}) (\d+|-)(?:\s"([^"]*)" "([^"]*)")?/');
function validate_apachenginx_combined_log_format(string $input): bool {
return (bool) preg_match(APACHENGINX_COMBINED_LOG_FORMAT_PATTERN, $input);
}
// Example
var_dump(validate_apachenginx_combined_log_format("192.168.1.1 - frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326 "http://www.example.com/start.html" "Mozilla/5.0"")); // bool(true)Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
192.168.1.1 - frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326 "http://www.example.com/start.html" "Mozilla/5.0" | not a log line |
10.0.0.1 - - [01/Jan/2024:00:00:00 +0000] "POST /api/v1/users HTTP/1.1" 201 450 | 192.168.1.1 [date] GET / 200 |
| — | plain text log |
When to use this pattern
This pattern is drawn from the Dev & Systems > Log Parsing 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
Log lines with escaped quotes (\" inside request field) will break naive parsers. Always handle escaped quotes inside quoted fields.
Technical Notes
Groups: 1=client IP, 2=auth user, 3=datetime, 4=method, 5=path, 6=status, 7=bytes, 8=referer, 9=user-agent. The - placeholder indicates missing values.
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