Help on redirects based on request url culture

Topics: Developer Forum, User Forum
Apr 23, 2007 at 8:49 PM
I have a situation where I need to route redirects based on culture of the request url. Is there a way I can handle this from the ini file?

Any suggestions would be greatly appreciated.

Problem Scenario:

1. should go to
2. should go to
3. should go to
4. should go to

Today I have it as the example below and the limitation is what ever be the request url culture , it always redirects to .com site:

RewriteRule ^/(?i)shop$ R,I

Apr 25, 2007 at 4:35 PM
You cannot distinguish among,, and, using only a RewriteRule statement. The RewriteRule only looks at the URL request, after the hostname. In order to distinguish between hostnames, you need a RewriteCond statement, and you need to evaluate the SERVER_NAME variable.

Check the README. Example 12 deals with this situation.

Example 12:
  RewriteCond %{SERVER_NAME}          ([^\.]+)\.chiesa\.net$                [I]
  RewriteCond c:\Inetpub\wwwroot\%1   !-d
  RewriteRule ^(.*)$                  /NotFound.htm                         [U,I,L]

This example rule says, look at the SERVER_NAME variable, and return TRUE if it matches the pattern given. In the case above, it says, check to see that it matches * In your case you would expect it to be,,, etc. (the pattern might be like this:
  RewriteCond %{SERVER_NAME}          ([^\.]+)\.pki\.(com|co\.uk|ca|de)$                [I]

The next (chained) RewriteCond says, if a directory on the server exists with the name that corresponds to the hostname used. For example, if the hostname is, then this RewriteCond looks for the directory c:\Inetpub\wwwroot\ The RewriteCond evaluates to TRUE if this directory does not exist. If both RewriteCond statements evaluate to true, then the RewriteRule is processed. the RewriteRule in this case always redirects every request to NotFound.htm.

That takes care of the case where the hostname is not known, or at least where the hostname used in the request does not have a corresponding filesystem directory in the place where you would expect. You can then use a similar rule (with 2 prefixed RewriteCond statements) that says, if the directory does exist, then redirect the request appropriately.

  RewriteCond %{SERVER_NAME}          ([^\.]+)\.pki\.(com|co\.uk|ca|de)$                [I]
  RewriteCond c:\Inetpub\wwwroot\%1   -d
  RewriteRule ^(.*)$                  /common/handler.aspx?url=$1&culture=%3    [U,I,L]

This rule says, if it is a known server name, and if the directory exists, then redirect to a common handler page. Pass the culture, which is the 3rd thing matched in the most recently evaluated rewritecond, as a query parameter named "culture".

Anyway, check the readme.

Last thing: You cannot use testdriver.exe to test rules that use RewriteCond statements that reference server variables. Because they depend on the server variables, you can only test them when the IIRF is loaded in the web server. This does not occur when testing rules in the testdriver. This is also noted in the readme. For testing of these kinds of things, you need to use IIRF with an IIS server.