How To Create a URL Rewrite Rule - Example

Unofficial Content

What follow is a basic sample of rewrite rules that will make the URL of your web page a more friendly one.

Specifically, this example shows what we have to do to take out the '.aspx' from the URL of a web panel.


Just follow this 3 steps:

(1) Open the archive web.config of your KB model.

(2) Insert the following code:

<system.webServer>
    <rewrite>
        <rules>
        <clear />
            <rule name="staticos" patternSyntax="Wildcard" stopProcessing="true">
                <match url="*.js" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                <action type="None" />
            </rule>
            <rule name="aspx resources" stopProcessing="true">
                <match url="(.+)\.aspx$" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
            </rule>
            <rule name="~ home ~" stopProcessing="true">
                <match url=".*execute.xml" />
                <action type="None" />
            </rule>
            <rule name="Pages" stopProcessing="true">
                <match url="^([^/]+)$" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                <action type="Rewrite" url="{R:1}.aspx" appendQueryString="true" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

(3) Save it and that´s it!

                                     
(You could also do the same directly from the IIS Manager as explined in this link: Creating Rewrite Rules for the URL Rewrite Module.)
 

As you see in the code, we are adding 4 rules. With those rules (and the procedure FriendlyLink), we are takeing out the '.aspx' at the end of the URL of web panel.
With the 3th one we are indicating that if the URL matches with ".*execute.xml" do no action. The first rule do the same but with the pages that ends in '.js'. If we take out those rules, when you write an URL that matches the pattern, the browser will add '.aspx' at the end of the URL because of the 2nd and 4th rule. This rules do that because we are taking out the '.aspx' with the procedure FriendlyLink (see FriendlyUrl at Basic Samples - Datatype), but we actually want to see a page that ends in '.aspx'.
 
Just picking the appropiate Regular Expressions you will be able rewrite the URL you wish.