when browse my site from local iis then address looks in browser address bar as http://localhost:8800/gb/default.aspx
i tried to extract country cod
Your rewrite action will not work as you back reference to parts of the match of the rule with {R:1}
and {R:2}
but this will never match as you negated it with negate="true"
. I.e. if it doesn't match that regular expression the {R:1}
and {R:2}
will (most likely) also not be available.
What you have to do is add an extra condition that matches on the query string with the same regular expression. This condition will make sure that the current URL will match that regular expression and then you can back reference parts of that with {C:1}
and {C:2}
, etc... (with the letter C
instead of R
). The trick is to use a combination of {C:1}
and {R:0}
. {C:1}
will give you the language code from the current URL and {R:0}
will give you the original URL from the <a href>
.
So you will end up with:
<outboundRules>
<rule name="add outbound rule" preCondition="Ishtml" enabled="true" stopProcessing="true">
<match filterByTags="A" pattern="^\/(?![a-z]{2}\/).*" />
<action type="Rewrite" value="{C:1}{R:0}" />
<conditions>
<add input="{HTTP_X_ORIGINAL_URL}" pattern="^(\/[a-z]{2})\/(.*)" />
</conditions>
</rule>
<preConditions>
<preCondition name="Ishtml">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="text/html" />
</preCondition>
</preConditions>
</outboundRules>
On a side note, your regular expression for the A tag is very weak. It will match /gb/default.aspx
but it will also match /foo/bar/default.aspx
or /foo/bar/gb/default.aspx
and thus not rewrite these links. I've replaced it with a more strict version only matching /<two letters>/<anything>
.
Note that I took the \/
after the match for the first two letters out of the first group in the condition as we don't need that slash in the back reference {C:1}
as it's already in {R:0}
.
EDIT: I made the assumption that {R:0}
would still be available as a back reference even if the regular expression would not match. This does not seem to be the case. So I took out the negate="true"
and fixed the regular expression to match any URL except those starting with /<two-letters/
(to prevent rewriting already correct URL's).
Another error was to match on {REQUEST_URI}
assuming this was the original URL as seen in the browser with the language code in it. But most obviously it's not as you probably (inbound) rewrite that URL and take out the language code. So I replaced that with {HTTP_X_ORIGINAL_URL}
. This is a variable set by the URL Rewrite module and preserves the original URL.
Lastly the <add ..>
in the <condition>...</condition>
was missing a closing /
. I hope this now works for you.