Keycloak "Invalid parameter: redirect_uri": how to fix it
Why Keycloak rejects a redirect_uri: exact and wildcard matching, http behind a proxy, post-logout URIs and the 26.7.3 rule on OIDC parameters, with fixes.
- Author
- The RealmDeploy team
- Published
"Invalid parameter: redirect_uri" is the error page Keycloak shows when an app sends users to sign in with a return address the client has not registered. The page appears before any login form. The user cannot get past it, and the app never hears about it, because Keycloak will not redirect to an address it does not trust.
It usually shows up right after a deploy: a new domain, a new path, a staging copy, or an app that moved behind a load balancer. It also shows up on logout, with a slightly different message, when the address in post_logout_redirect_uri is not allowed.
This guide covers Keycloak 26.x. The strings quoted below are from the Keycloak 26.8.2 source.
What you see
On sign-in, the browser lands on a Keycloak error page with HTTP 400 and this text:
Invalid parameter: redirect_uri
On logout, the error page says:
Invalid redirect uri
In the realm's events, sign-in failures are LOGIN_ERROR events with error set to invalid_redirect_uri and a redirect_uri detail that holds the address the app sent. Logout failures are LOGOUT_ERROR events with the same error.
If the address was refused because it carries an OIDC response parameter, the server log also has a WARN line that starts with:
Redirect URI rejected: contains forbidden OIDC parameter
followed by the parameter name and where it was found (query or fragment).
Causes, most common first
- The address is not in Valid redirect URIs. A new domain, port or path that nobody added. The
redirect_uridetail in the event shows the exact string. - A small difference from an exact entry. A trailing slash,
httpinstead ofhttps, a different port, a query string, a different host spelling. The two strings look the same until you put them side by side. - The app builds
http://behind a TLS-terminating proxy. The app thinks it serves plain HTTP because the proxy talks to it that way. The address in the event starts withhttp://while users seehttps://. - A wildcard that does not do what you expect. A
*only works at the end of an entry.https://*.example.com/callbackis a literal string, not a subdomain wildcard. - The address contains an OIDC response parameter. Since Keycloak 26.7.3, redirect URIs that carry parameters such as
state,codeorsession_stateare refused. The WARN line above confirms it. - A logout address that is not allowed. The logout page says "Invalid redirect uri". Valid post logout redirect URIs does not include the address.
- The wrong client. The
client_idin the request is a different client than the one you edited, often a copy for another environment.
How Keycloak matches the address
Knowing the rules saves most of the time spent on this error. In Keycloak 26.x:
- Exact entries must equal the redirect URI character for character: scheme, host, port, path, trailing slash and query string.
- A trailing
*matches any address that starts with the text before it. The query and fragment of the request are ignored for this comparison.https://app.example.com/app/*also acceptshttps://app.example.com/app. - An entry with a
?or#is never treated as a wildcard. - A
*anywhere else is a plain character. There are no subdomain wildcards. - A lone
*accepts any address. Do not use it outside a local test realm. - Relative entries that start with
/are joined to the client's Root URL. - Loopback addresses with
httponlocalhost,127.0.0.1or[::1]are also tried on the default port, so a native app can listen on any port while the client registers the address without one. - Wildcards are switched off for addresses with user info or
..path segments. Those can only match an exact entry.
Diagnose it step by step
- Turn on saved events. In the admin console, open Realm settings, then the Events tab, and under User events settings switch on Save events.
LOGIN_ERRORandLOGOUT_ERRORare saved by default. - Reproduce the error. Open Events in the left menu. Find the
LOGIN_ERRORwithinvalid_redirect_uriand copy theredirect_uridetail. That is the exact string Keycloak checked. - Copy the client's list. Open Clients, pick the client named in the event, and copy Valid redirect URIs and Valid post logout redirect URIs from the Settings tab, under Access settings.
- Compare the two strings side by side. Look for the scheme, the port, a trailing slash, a query string and the host.
- Check the server log. Search for "Redirect URI rejected" to catch the forbidden-parameter case.
- Read the authorization URL your app builds. Log it once, then decode it:
python3 -c "import sys, urllib.parse as u; q=u.urlparse(sys.argv[1]).query; print(u.parse_qs(q)['redirect_uri'][0])" \
'https://auth.example.com/realms/myrealm/protocol/openid-connect/auth?client_id=my-app&redirect_uri=https%3A%2F%2Fapp.example.com%2Fcallback&response_type=code&scope=openid'
Fixes
Add the exact address
The safest fix is an exact entry for every address the app really uses. With kcadm.sh:
kcadm.sh config credentials --server https://auth.example.com --realm master --user admin
CID=$(kcadm.sh get clients -r myrealm -q clientId=my-app --fields id --format csv --noquotes)
kcadm.sh update clients/$CID -r myrealm \
-s 'redirectUris=["https://app.example.com/callback","https://app.example.com/silent-renew"]'
The update replaces the whole list, so include every entry you want to keep.
Prefer a narrow wildcard over a broad one
If an app needs many callback paths, end the entry at a path you own:
https://app.example.com/auth/*
Do not use https://app.example.com*: it also accepts https://app.example.com.attacker.example/.
Fix http behind a proxy
Make the app build its redirect URI from configuration, not from the incoming request. If your framework derives it from the request, tell the framework to trust the proxy's X-Forwarded-Proto header. For a Node app on Express:
app.set('trust proxy', 1);
Then check the event again. The address should start with https://.
Remove OIDC parameters from the address
Keycloak 26.7.3 rejects redirect URIs and post_logout_redirect_uri values that contain OIDC response parameters, in the query or in the fragment. The parameters it refuses include code, state, session_state, id_token, access_token, token_type, expires_in, iss, error and error_description. They clash with the parameters Keycloak adds when it redirects, which opens the door to HTTP parameter pollution. The upgrading guide describes the change.
The common case is a post_logout_redirect_uri that points straight at the Keycloak login URL with response_type=code&client_id=... in it. Replace it with a callback in your app, and let that callback start a new sign-in:
post_logout_redirect_uri=https://app.example.com/logout-callback
If you must keep the old behavior while you migrate, the upgrading guide names a server option, allow-oidc-params-in-redirect-uris, and a per-client attribute, allow.oidc.params.in.redirect.uris. Both are deprecated and will be removed in Keycloak 27, so treat them as a short bridge.
Fix logout addresses
Valid post logout redirect URIs has its own list. Two values are special:
+adds every entry from Valid redirect URIs to the list.-allows no post-logout address at all.
When the list is empty, Keycloak uses Valid redirect URIs. A logout request also needs an id_token_hint or a client_id so Keycloak knows which client's list to check:
https://auth.example.com/realms/myrealm/protocol/openid-connect/logout
?client_id=my-app
&post_logout_redirect_uri=https%3A%2F%2Fapp.example.com%2F
On RealmDeploy
Your Keycloak answers on your own hostname over HTTPS, so every redirect URI you register should start with https://, except loopback addresses for local development. Plain HTTP requests to your hostname are redirected to HTTPS. See Custom domains and TLS.
Valid redirect URIs, Valid post logout redirect URIs and Root URL are client settings in your own admin console. We do not change them. Tenants run Keycloak 26.8.1, so the 26.7.3 rule on OIDC parameters applies to every client. See Keycloak versions.
If an address still fails after these steps, write to us from the dashboard or through the contact form with the event details.
Tools that help
- Redirect URI matcher: paste the client's list and the address from the event to see which entry matches, and why not.
- Authorization URL inspector: break an authorization URL into its parameters and read
redirect_uridecoded. - Authorization URL builder: build a clean authorization request for testing.
- Logout URL builder: build an RP-initiated logout URL with
post_logout_redirect_uriandclient_id. - Callback URL parser: read what came back on the callback, including
erroranderror_description. - OIDC client generator: generate a client definition with redirect URIs and web origins.
- kcadm.sh command builder: build the
update clientscommand without quoting mistakes. - Hostname settings helper: check the hostname options when addresses come out with the wrong scheme or host.
Keep reading
Keycloak CORS errors: causes and fixes
Why the browser blocks calls to Keycloak with a CORS error, how Web origins work, how to find the real status code behind it, and how to fix each cause.
Keycloak "HTTPS required": causes and safe fixes
What the Keycloak "HTTPS required" page means, how Require SSL decides, how to get back into a locked master realm, and why turning SSL off is the wrong fix.
Keycloak invalid_grant: causes and fixes
Why the Keycloak token endpoint answers invalid_grant: reused or expired codes, redirect_uri mismatches, ended sessions, stale refresh tokens and PKCE.