Azure环境下web.config实现WordPress指定URL重定向的技术问询
Got it, let's break down why your current rewrite rule isn't working and get that redirect sorted properly for both example.com and www.example.com.
Why Your Existing Rule Fails
IIS's URL Rewrite module matches against the decoded request path—meaning it converts URL-encoded characters like %E4 back to their actual Unicode equivalents (in this case, ä) before checking the rule. Your current rule uses encoded characters (%E4) and incorrect escaping, so it never matches the actual request path.
Additionally, the + character is a special regex symbol (it means "match one or more of the previous character"), so you need to escape it with \+ to match the literal + in your old URL.
The Correct web.config Rule
Here's the rule that will handle both the example.com and www.example.com domains, and properly match your old URL:
<rule name="Redirect Vad+är+Vårdskap Page" stopProcessing="true"> <!-- Match the decoded path: Vad+är+Vårdskap__1053.html --> <match url="^Vad\+är\+Vårdskap__1053\.html$" ignoreCase="true" /> <!-- Ensure we only redirect requests to example.com or www.example.com --> <conditions> <add input="{HTTP_HOST}" pattern="^(www\.)?example\.com$" /> </conditions> <!-- Permanent 301 redirect to the new URL --> <action type="Redirect" url="/what-is-welcoming/" redirectType="Permanent" /> </rule>
Alternative: Unicode Escape for Special Characters
If you're worried about server character encoding issues (unlikely on Azure, but safe to use), you can replace the ä characters with their Unicode regex escape sequences (\u00E4):
<rule name="Redirect Vad+är+Vårdskap Page" stopProcessing="true"> <match url="^Vad\+\u00E4r\+V\u00E4rdskap__1053\.html$" ignoreCase="true" /> <conditions> <add input="{HTTP_HOST}" pattern="^(www\.)?example\.com$" /> </conditions> <action type="Redirect" url="/what-is-welcoming/" redirectType="Permanent" /> </rule>
How to Test & Verify
- Save the updated
web.configfile in your WordPress root directory on Azure. - IIS should pick up the change automatically, but you can restart your App Service instance to be safe.
- Test both:
http://example.com/Vad+%E4r+V%E4rdskap__1053.htmlhttp://www.example.com/Vad+%E4r+V%E4rdskap__1053.html
Both should redirect permanently tohttp://[your-domain]/what-is-welcoming/.
内容的提问来源于stack exchange,提问作者Emil Jönsson

