Thursday, April 28, 2011

URL Rewrite language querystring

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?

From stackoverflow
  • 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>
    
    MartinHN : That doesn't seem to work. Gives me 404 with example.com/en/contact.aspx
    Remy Lebeau - TeamB : 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?
  • 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>
    
    MartinHN : Doesn't work. It just gives me 404, doesn't really rewrite.
  • 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>
    

0 comments:

Post a Comment