@@ -1177,6 +1177,7 @@ export class RouterCore<
11771177 // Before we do any processing, we need to allow rewrites to modify the URL
11781178 // build up the full URL by combining the href from history with the router's origin
11791179 const fullUrl = new URL ( href , this . origin )
1180+
11801181 const url = executeRewriteInput ( this . rewrite , fullUrl )
11811182
11821183 const parsedSearch = this . options . parseSearch ( url . search )
@@ -1187,11 +1188,33 @@ export class RouterCore<
11871188
11881189 const fullPath = url . href . replace ( url . origin , '' )
11891190
1191+ // Save the internal pathname for route matching (before output rewrite)
1192+ const internalPathname = url . pathname
1193+
1194+ // Compute publicHref by applying the output rewrite.
1195+ //
1196+ // The publicHref represents the URL as it should appear in the browser.
1197+ // This must match what buildLocation computes for the same logical route,
1198+ // otherwise the server-side redirect check will see a mismatch and trigger
1199+ // an infinite redirect loop.
1200+ //
1201+ // We always apply the output rewrite (not conditionally) because the
1202+ // incoming URL may have already been transformed by external middleware
1203+ // before reaching the router. In that case, the input rewrite has nothing
1204+ // to do, but we still need the output rewrite to reconstruct the correct
1205+ // public-facing URL.
1206+ //
1207+ // Clone the URL to avoid mutating the one used for route matching.
1208+ const urlForOutput = new URL ( url . href )
1209+ const rewrittenUrl = executeRewriteOutput ( this . rewrite , urlForOutput )
1210+ const publicHref =
1211+ rewrittenUrl . pathname + rewrittenUrl . search + rewrittenUrl . hash
1212+
11901213 return {
11911214 href : fullPath ,
1192- publicHref : href ,
1215+ publicHref,
11931216 url : url ,
1194- pathname : decodePath ( url . pathname ) ,
1217+ pathname : decodePath ( internalPathname ) ,
11951218 searchStr,
11961219 search : replaceEqualDeep ( previousLocation ?. search , parsedSearch ) as any ,
11971220 hash : url . hash . split ( '#' ) . reverse ( ) [ 0 ] ?? '' ,
0 commit comments