URL重写求助:带匿名GUID参数的URL跳转出现404错误
Got it, let's sort out this 404 issue with your IIS rewrite configuration. The root problem is your current RewriteMap setup only matches the exact path /car/details/, but your actual requests include a dynamic GUID at the end. Since the map can't find a matching key for the full URI (like /car/details/4f2a95ed-3582-4486-8ef6-8a8e6731161f), the rule never triggers, resulting in that 404 error.
Here are two solid solutions depending on whether you want to keep using RewriteMap or simplify with a direct rule:
Solution 1: Direct Rule (No RewriteMap Needed)
This is the simplest approach if you only need to handle this specific URL pattern. We'll use a regular expression to capture the dynamic GUID and inject it into the target URL:
<rewrite> <rules> <!-- Match URLs with the format /Car/Details/{GUID} --> <rule name="Car Details Rewrite" stopProcessing="true"> <match url="^Car/Details/([0-9a-fA-F-]+)$" /> <!-- Rewrite to the target page, passing the captured GUID as a query string parameter --> <action type="Rewrite" url="/Car/index.htm?Action=Details&guid={R:1}" /> </rule> </rules> </rewrite>
Key Details:
- The regex
^Car/Details/([0-9a-fA-F-]+)$specifically matches GUIDs (hex characters plus hyphens in the standard GUID format). {R:1}references the first captured group (the GUID) from the match pattern.stopProcessing="true"ensures no other rewrite rules run after this one, preventing conflicts.
Solution 2: Keep Using RewriteMap (For Scalability)
If you need to manage multiple similar rewrite mappings, you can adjust your rule to separate the static path prefix (for the RewriteMap) and the dynamic GUID:
<rewrite> <rules> <rule name="Rewrite rule1 for carAPI" stopProcessing="true"> <!-- Capture the GUID from the end of the URL --> <match url="^Car/Details/([0-9a-fA-F-]+)$" /> <conditions> <!-- Look up the static path prefix in your RewriteMap --> <add input="{carAPI:/Car/Details/}" pattern="(.+)" /> </conditions> <!-- Append the captured GUID to the mapped target URL --> <action type="Rewrite" url="{C:1}&guid={R:1}" appendQueryString="false" /> </rule> </rules> <rewriteMaps> <rewriteMap name="carAPI"> <add key="/Car/Details/" value="/Car/index.htm?Action=Details" /> <!-- Add other mappings here if needed --> </rewriteMap> </rewriteMaps> </rewrite>
Key Details:
- We use the fixed path
/Car/Details/to look up the RewriteMap (instead of the full request URI), so the map finds a match. {C:1}references the value from the RewriteMap condition.appendQueryString="false"disables automatic query string appending since we're manually adding theguidparameter.
Either of these setups will correctly pass the dynamic GUID to your target URL and eliminate the 404 error.
内容的提问来源于stack exchange,提问作者Great Big Al

