{"record":{"id":"d12b6cc0023d64c4","repo":"symfony/routing","slug":"cannot-mix-utf-8-requirements-with-non-utf-8-pattern-s","errorCode":null,"errorMessage":"Cannot mix UTF-8 requirements with non-UTF-8 pattern \"%s\".","messagePattern":"Cannot mix UTF-8 requirements with non-UTF-8 pattern \"(.+?)\"\\.","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"RouteCompiler.php","lineNumber":121,"sourceCode":"            array_unique($variables)\n        );\n    }\n\n    private static function compilePattern(Route $route, string $pattern, bool $isHost): array\n    {\n        $tokens = [];\n        $variables = [];\n        $matches = [];\n        $pos = 0;\n        $defaultSeparator = $isHost ? '.' : '/';\n        $useUtf8 = preg_match('//u', $pattern);\n        $needsUtf8 = $route->getOption('utf8');\n\n        if (!$needsUtf8 && $useUtf8 && preg_match('/[\\x80-\\xFF]/', $pattern)) {\n            throw new \\LogicException(\\sprintf('Cannot use UTF-8 route patterns without setting the \"utf8\" option for route \"%s\".', $route->getPath()));\n        }\n        if (!$useUtf8 && $needsUtf8) {\n            throw new \\LogicException(\\sprintf('Cannot mix UTF-8 requirements with non-UTF-8 pattern \"%s\".', $pattern));\n        }\n\n        // Match all variables enclosed in \"{}\" and iterate over them. But we only want to match the innermost variable\n        // in case of nested \"{}\", e.g. {foo{bar}}. This in ensured because \\w does not match \"{\" or \"}\" itself.\n        preg_match_all('#\\{(!)?([\\w\\x80-\\xFF]+)\\}#', $pattern, $matches, \\PREG_OFFSET_CAPTURE | \\PREG_SET_ORDER);\n        foreach ($matches as $match) {\n            $important = $match[1][1] >= 0;\n            $varName = $match[2][0];\n            // get all static text preceding the current variable\n            $precedingText = substr($pattern, $pos, $match[0][1] - $pos);\n            $pos = $match[0][1] + \\strlen($match[0][0]);\n\n            if (!\\strlen($precedingText)) {\n                $precedingChar = '';\n            } elseif ($useUtf8) {\n                preg_match('/.$/u', $precedingText, $precedingChar);\n                $precedingChar = $precedingChar[0];\n            } else {","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/symfony/routing/blob/83fa223250b50f4f018c011e101c330e65ac63cc/RouteCompiler.php#L103-L139","documentation":"The reverse of error 72: the route's \"utf8\" option is set to true but the pattern itself is not valid UTF-8 (preg_match('//u', $pattern) fails). Mixing UTF-8 requirements with a non-UTF-8 pattern would produce inconsistent regexes, so compilePattern throws a LogicException.","triggerScenarios":"Compiling a route with options ['utf8' => true] whose path or host pattern is invalid or non-UTF-8 encoded bytes (e.g. a Latin-1 encoded config file containing '/café').","commonSituations":"Config files saved with the wrong encoding (ISO-8859-1 instead of UTF-8); routes built from external data with broken encoding; enabling utf8 globally but having legacy ASCII-escaped patterns.","solutions":["Re-save the route definition file with UTF-8 encoding.","Fix the pattern string so it is valid UTF-8 (check with mb_check_encoding($path, 'UTF-8')).","Remove the utf8 => true option if the pattern is intentionally non-UTF-8 ASCII.","Normalize input with mb_convert_encoding($path, 'UTF-8', 'ISO-8859-1') before creating the route."],"exampleFix":"// before (pattern bytes are Latin-1)\n$route = new Route(\"/caf\\xE9/{name}\", options: ['utf8' => true]);\n\n// after (valid UTF-8)\n$route = new Route('/café/{name}', options: ['utf8' => true]);","handlingStrategy":"validation","validationCode":"if ($route->getOption('utf8') && !mb_check_encoding($route->getPath(), 'UTF-8')) {\n    throw new \\RuntimeException('Route path is not valid UTF-8 but the utf8 option is enabled.');\n}","typeGuard":null,"tryCatchPattern":"try {\n    (new RouteCompiler())->compile($route);\n} catch (\\LogicException $e) {\n    // fix encoding (mb_convert_encoding) or drop the utf8 option, then retry\n}","preventionTips":["Enforce UTF-8 file encoding (and BOM-free) for all routing config via CI checks.","Validate external/user-supplied strings with mb_check_encoding before building routes.","Normalize strings with mb_convert_encoding when importing route data."],"tags":["routing","symfony","utf8","encoding"],"backgroundTag":"conflicting-config-options","analyzedSha":"83fa223250b50f4f018c011e101c330e65ac63cc","analyzedAt":"2026-09-14T03:19:46.280Z","contentChangedAt":"2026-09-14T03:19:46.280Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}