argyle
argyle

Reputation: 1339

IIS URL Rewrite

I have the following rewrite rule:

    <rewrite>
        <rules>
            <rule name="FrontController" stopProcessing="true">
                <match url="^(.*)$" ignoreCase="false" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                </conditions>
                <action type="Rewrite" url="wcf/api.svc/auth/home" />
            </rule>
        </rules>
    </rewrite>

This basically rewrites all non-file urls to web service api calls that returns index.html in an SPA backed by WCF.

The above rewrite ends up including all the query string parameters that were included with the original URL. What I need to do is also include the original URL, such as, 'wcf/api.svc/auth/products', as a query string parameter in the rewritten URL, such as 'https://domain.com/wcf/api.svc/auth/products?enc=lkjewro8xlkz' being transformed into 'https://domain.com/wcf/api.svc/auth/home?enc=lkjewro8xlkz&orig=wcf/api.svc/auth/products'.

Is this possible, and if so, what changes would I need to make to achieve it? I would like for my WCF application to know about the original URL so that it can configure the SPA to initialize to a particular view on load.

Thanks

Upvotes: 0

Views: 280

Answers (1)

Kul-Tigin
Kul-Tigin

Reputation: 16950

It's quite possible.

You need to add the URL Encoded value of the {REQUEST_URI} to the Rewrite URL.

<rewrite>
    <rules>
        <rule name="FrontController" stopProcessing="true">
            <match url="^(.*)$" ignoreCase="false" />
            <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            </conditions>
            <action type="Rewrite" url="wcf/api.svc/auth/home?orig={UrlEncode:{REQUEST_URI}}" />
        </rule>
    </rules>
</rewrite>

With this rule, in your WCF endpoint orig parameter would be:

/wcf/api.svc/auth/products?enc=lkjewro8xlkz

If you don't want the query string part (?enc=lkjewro8xlkz), you'll need an extra condition to match the URI without query string.

<rewrite>
    <rules>
        <rule name="FrontController" stopProcessing="true">
            <match url="^(.*)$" ignoreCase="false" />
            <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />

                <!-- match any character up to a question mark -->
                <add input="{REQUEST_URI}" pattern="^[^\?]+" />
            </conditions>

            <!-- {C:0} means the first match in conditions -->
            <action type="Rewrite" url="wcf/api.svc/auth/home?orig={UrlEncode:{C:0}}" />
        </rule>
    </rules>
</rewrite>

Now, orig will be /wcf/api.svc/auth/products in the WCF endpoint.

Hope it helps.

Upvotes: 1

Related Questions