What a redirect URI is
In the authorization code flow, the user signs in at the authorization server, and the server sends the browser back to the app with a code. The address it sends the browser to is the redirect URI. The app names it in the redirect_uri parameter of the authorization request.
The code in that redirect is valuable. Together with the client’s credentials or PKCE verifier, it buys tokens. So the server must never send it to an address the client did not register. RFC 6749 section 3.1.2 defines the redirection endpoint and requires the server to check the requested URI against the registered ones.
In Keycloak, the registered list is the client’s Valid redirect URIs setting. Every authorization request is checked against it before any sign-in page is shown.
Why strict matching matters
Loose matching is one of the oldest ways to steal authorization codes. If a server accepts any URI that starts with https://app.example.com, an attacker can try https://app.example.com.attacker.example/ or an open redirect on the real site, and the code lands somewhere it can be read.
RFC 9700, the OAuth security best current practice, says servers must compare redirect URIs by exact string matching. The one exception it allows is the port number of a localhost redirect URI for native apps. Wildcards are a convenience that goes against this, which is why it is worth knowing exactly how your server treats them.
How Keycloak matches a redirect URI
Keycloak checks the redirect_uri from the request against each entry in Valid redirect URIs. The rules below describe Keycloak 26.x as we understand them. They are what the matcher models.
Exact entries
An entry without a trailing * must equal the redirect URI character for character. That includes the scheme, the host’s spelling and case, the port, the path, a trailing slash and the query string. https://app.example.com/callback does not match https://app.example.com/callback?next=/home.
A trailing wildcard
An entry that ends in * matches any redirect URI that starts with the text before the *. For this comparison Keycloak ignores the query and fragment of the redirect URI. If the text before the * ends in a slash, the URI without that slash also matches, so https://app.example.com/admin/* accepts https://app.example.com/admin.
An entry that contains a ? is not treated as a wildcard. Its * is then a plain character.
A star anywhere else
Only a * at the very end is a wildcard. A star in the host, such as https://*.example.com/callback, is a plain character and matches only that literal text. Keycloak has no subdomain wildcards.
When wildcards are switched off
If the redirect URI has user info (a name before the host, followed by an at sign) or a path with a parent-folder step such as .. or its encoded form %2e%2e, Keycloak does not apply wildcard entries to it. Such a URI can then match only an exact entry. This closes a path-traversal trick that turned an allowed prefix into a way out of it.
Relative entries and the root URL
An entry that starts with / is joined to the client’s Root URL, as written. With Root URL https://app.example.com, the entry /silent-check-sso.html becomes https://app.example.com/silent-check-sso.html. With no Root URL, Keycloak uses the origin of its own server URL.
Loopback ports
For redirect URIs that start with http://localhost or http://127.0.0.1, Keycloak also tries the URI with its port removed. A native app can then listen on any free port, as RFC 8252 recommends, while the client registers http://127.0.0.1/callback.
An example
Here is a sample client, made up for this page, and how five redirect URIs fare against it.
https://app.example.com/callback
https://app.example.com/admin/*
/silent-check-sso.html
http://localhost/*| redirect_uri | Result | Why |
|---|---|---|
https://app.example.com/callback | Match | Equal to the first entry. |
https://app.example.com/admin/reports?tab=1 | Match | Starts with the wildcard prefix; the query is ignored. |
https://app.example.com/callback?next=/home | No match | An exact entry must include the query. |
http://localhost:8123/cb | Match | The loopback port is ignored, then the wildcard applies. |
https://app.example.com/admin/%2e%2e/private | No match | Encoded .. turns wildcards off. |
What this tool does
The matcher takes a redirect URI, a list of valid redirect URIs, the client’s Root URL and your Keycloak server URL. It applies the rules above and shows:
- An overall verdict: whether Keycloak would accept the URI, and which entry matched.
- The URI as compared, after dot segments such as
/a/../are removed. - Whether wildcards are allowed for this URI, and if not, why.
- The port-less form it tried for a loopback address.
- A row per entry: match or no match, exact or wildcard, and the reason.
- Warnings for a lone
*, a*in the middle of an entry, plain http outside localhost, a fragment, or capital letters in the host.
Example buttons fill in common cases, so you can see how queries, dot segments, encoded dots and loopback ports behave.
Limits of the check
The matcher models Keycloak 26.x’s rules; it does not run Keycloak’s code. It does not model every percent-encoding case, the order in which Keycloak tries entries, or client policies that add their own conditions. Use it to understand and debug, then test the final setup in your realm.
How to use the matcher
- Copy the
redirect_uriyour app sends. The authorization URL inspector can pull it out of a full authorization URL. - Paste it into the first field.
- Copy the client’s Valid redirect URIs from the Keycloak admin console and paste them, one per line.
- Fill in the client’s Root URL if any entry starts with
/. - Read the verdict, then the row for each entry to see why it did or did not match.
Setting up redirect URIs by app type
Web apps with a back end
Register the exact callback URL for each environment, such as production and staging. One app rarely needs a wildcard; if routing sends users back to different pages, keep one callback and store the return path in your session.
Single-page apps
Register the exact callback and, if you use silent checks, the silent check page. Remember that Web origins is a separate setting: it controls which origins may call Keycloak from the browser, not where Keycloak may redirect.
Mobile apps
Use a claimed https link or a private URI scheme such as com.example.app:/callback, and register it exactly. A private scheme can be claimed by another app on the same device, so pair it with PKCE.
Desktop apps and command-line tools
Listen on a loopback address with a free port and register http://127.0.0.1/callback or http://localhost/callback. Keycloak ignores the port for these, so the app does not need a fixed one.
Errors you will see
Invalid parameter: redirect_uri
Keycloak shows this on an error page when no entry matches. It does not redirect, because it cannot trust the URI. Compare the two values character by character: a trailing slash, http versus https, a port, or a query string are the usual causes.
invalid_grant at the token endpoint
The redirect_uri in the token request must be identical to the one in the authorization request. If they differ, the code exchange fails even though sign-in worked.
Invalid redirect uri on logout
Logout uses post_logout_redirect_uri, checked against Valid post logout redirect URIs. A + there means the same list as Valid redirect URIs. See the logout URL builder.
Web origins are a different setting
Keycloak clients also have Web origins. It decides which browser origins may call Keycloak’s endpoints with CORS, for example a single-page app calling the token endpoint. In Web origins, + means the origins of the valid redirect URIs, and * allows every origin.
A + or * in Web origins has no effect on redirects. A redirect URI that fails the match stays rejected whatever Web origins says.
Security practices
- Prefer exact entries. Every wildcard widens where a code can be sent.
- Never use a lone
*in production. It lets the client send codes anywhere. - If you need a wildcard, keep the prefix as long as possible and end it in a path, such as
https://app.example.com/auth/*, nothttps://app.example.com*. - Use https everywhere except loopback addresses.
- Do not put a fragment in a redirect URI; RFC 6749 does not allow it.
- Use PKCE, so a code that leaks cannot be redeemed without the verifier.
- Remove entries for environments and apps you no longer run.