views:

467

answers:

3

Hi

I want to rewirte my querystring for selected language.

I have this URL: www.example.com/?lang=en, and want it to be www.example.com/en

It should rewrite on all pages. So www.example.com/contact.aspx?lang=en would be www.example.com/en/contact.aspx

Is there a general rewrite rule for this?

A: 

my syntax may be a little off but you could probably do something like this:

<rewrite>
  <rules>
    <rule name="Rewrite Language">
      <match url="/([a-z]+)/([_0-9a-z-]+)" />
      <action type="Rewrite" url="{R:2}?lang={R:1}" />
    </rule>
  </rules>
</rewrite>
Russ Bradberry
That doesn't seem to work. Gives me 404 with example.com/en/contact.aspx
MartinHN
I haven't played with URL re-writting yet, but wouldn't the action tag's url need a leading '/' in front to match the leading '/' in the match tag's url?
Remy Lebeau - TeamB
A: 

Please consider using the following:

<rewrite>
  <rules>
    <rule name="Rewrite Language">
      <match url="/([a-z]{2})(.*)" />
      <action type="Rewrite" url="{R:2}?lang={R:1}" />
    </rule>
  </rules>
</rewrite>
TonyCool
Doesn't work. It just gives me 404, doesn't really rewrite.
MartinHN
+1  A: 

This one works.

<rule name="Rewrite Language">
    <match url="([a-z]{2})(.*)" />
    <conditions logicalGrouping="MatchAll">
     <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
     <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="{R:2}?lang={R:1}" />
</rule>
MartinHN