借助web.config实现基于域名与语言参数的跳转配置
Troubleshooting Domain-to-Culture URL Rewrite Rules in web.config
Hey Tony, let's dig into why your URL rewrite rules for mapping domains to specific cultures aren't working. I’ve tackled similar multi-language domain setups before, so here’s what to check and how to fix it:
Common Mistakes to Look For
First, let’s rule out the most frequent misconfigurations:
- Rule Order Issues: Rewrite rules run in the order they’re listed. If you have a catch-all rule earlier in the list, it might block your domain-specific rules. Always put your domain-mapping rules at the top of the
<rules>section. - Incorrect Domain Matching: Your pattern for
{HTTP_HOST}might be too strict or missing case insensitivity. Domains are case-insensitive, so addingignoreCase="true"is crucial. - Missing Query String Handling: If you’re not preserving existing query parameters, you might break other functionality, or your app might not pick up the
TemplateCultureparameter correctly. - Uninstalled URL Rewrite Module: IIS won’t process these rules if the URL Rewrite module isn’t installed—double-check it’s present in your IIS Manager.
Correct web.config Rewrite Rule Example
Here’s a tested configuration that maps your domains to the right TemplateCulture parameters, while preserving existing query strings:
<configuration> <system.webServer> <rewrite> <rules> <!-- Map English domain to en-CA culture --> <rule name="English Domain to en-CA" stopProcessing="true"> <match url="^(.*)$" /> <!-- Match any URL path --> <conditions> <add input="{HTTP_HOST}" pattern="^www\.englishsite\.com$" ignoreCase="true" /> </conditions> <action type="Rewrite" url="{R:1}?TemplateCulture=en-CA{QUERY_STRING}" appendQueryString="false" /> </rule> <!-- Map French domain to fr-CA culture --> <rule name="French Domain to fr-CA" stopProcessing="true"> <match url="^(.*)$" /> <conditions> <add input="{HTTP_HOST}" pattern="^www\.frenchsite\.com$" ignoreCase="true" /> </conditions> <action type="Rewrite" url="{R:1}?TemplateCulture=fr-CA{QUERY_STRING}" appendQueryString="false" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
Key Details in This Configuration:
stopProcessing="true": Ensures once a domain rule matches, no other rules run (prevents conflicts).{R:1}: Captures the original URL path (e.g.,/aboutstays/aboutafter rewrite).{QUERY_STRING}: Preserves any existing query parameters (e.g.,?foo=barbecomes&foo=barafter addingTemplateCulture).appendQueryString="false": Disables automatic query string appending—since we’re manually adding{QUERY_STRING}, this avoids duplicate parameters.
How to Verify the Setup
- Check IIS Module: Open IIS Manager, look for the "URL Rewrite" icon under your site. If it’s missing, install the IIS URL Rewrite Module via the IIS Manager’s Web Platform Installer.
- Test Rule Patterns: In IIS, go to URL Rewrite → select your rule → click "Test Pattern". Enter
www.englishsite.comto confirm it matches the condition. - Enable Rewrite Logs: Turn on URL Rewrite logging to see if rules are triggering. Go to URL Rewrite → "View Server Variables" → enable logging. Check logs in
%SystemDrive%\inetpub\logs\LogFiles\W3SVC{n}for details on what’s being rewritten. - Validate App Logic: Make sure your application is actually reading
Request.QueryString["TemplateCulture"]to set the culture. Sometimes the issue is in the app code, not the rewrite rule.
Edge Cases to Consider
- Non-WWW Domains: If users might access
englishsite.com(without www), add a condition to match that too, or add a separate rule to redirect non-www to www first. - Default Documents: The rule above works for default docs like
index.aspxbecause^(.*)$matches empty paths (which map to default docs). - HTTPS: If your sites use HTTPS, ensure the rules work with the
HTTPSserver variable if needed (though the above config doesn’t require it unless you have HTTPS-only redirects).
内容的提问来源于stack exchange,提问作者Tony Chestnut
相关产品推荐
相关产品推荐

