I download the URL Rewrite module for IIS 7. I'm trying to rewrite url of this form: http://www.site.com/1234 to http://www.site.com/234 (get the number and rewrite its modulu by 1000).

I saw that the rewrite module supports regex but I didn't find where to apply mathematical operations (if possible) to the URL.

I'll be glad to get help with this! Thanks :)

url rewrite module link

link|improve this question
feedback

migrated from superuser.com Nov 12 '10 at 7:20

This question came from our site for computer enthusiasts and power users.

1 Answer

Unfortunately out-of-the box it does not have support for that, however you can use a few lines of C# to do that. You can follow the tutorial at: http://learn.iis.net/page.aspx/804/developing-a-custom-rewrite-provider-for-url-rewrite-module/

Basically you would include a rule conceptually like this one:
<rule name="Modulus Rewrite">
<match url="(.*)/([\d+])$" />
<action type="rewrite" url="{R:1}{Modulus:{R:2}}" />
</rule>

where you capture the URL, but separately the segment with numbers on it, and pass that to the custom provider

And then your code would look something like the one below and you will use a <condition > to

public class ModulusProvider: IRewriteProvider {

public void Initialize(IDictionary<string, string> settings, IRewriteContext rewriteContext)
{
}

public string Rewrite(string value)
{    
    int iVal;
    if (int.TryParse(value, out iVal)) {
        return (iVal % 1000).ToString(CultureInfo.InvariantCulture);
    }

    return String.Empty;
}

}

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.