1

I Have some problems with redirecting to another URL based on the query string parameters. I have url for mapping service, for example "http://www.mydomain.com/?lon=111&lat=222&zoom=3" and I need it to be redirected to "http://www.mydomain.com/111/222/3".

I have this rule in my web.config, but it does not work

<rule name="Location redirect">
    <match url="\?lon=(\d+)&amp;lat=(\d+)&amp;zoom=(\d+)" />
    <action type="Redirect" url="{R:1}/{R:2}/{R:3}" redirectType="Found" />
</rule>

1 Answer 1

3

The reason is that URL does not include Query String, you ned to use conditions for that. If you use the User Interface, it includes a friendly URL template that will generate it for you. Below you will find the Rewrite rule as well as the redirect so that legacy traffic (ugly URL) is redirected to the pretty URL:

            <rule name="RedirectUserFriendlyURL1" stopProcessing="true">
                <match url="^$" />
                <conditions>
                    <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                    <add input="{QUERY_STRING}" pattern="^lon=([^=&amp;]+)&amp;lat=([^=&amp;]+)&amp;zoom=([^=&amp;]+)$" />
                </conditions>
                <action type="Redirect" url="/{C:1}/{C:2}/{C:3}" appendQueryString="false" />
            </rule>
            <rule name="RewriteUserFriendlyURL1" stopProcessing="true">
                <match url="^/([^/]+)/([^/]+)/([^/]+)/?$" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                </conditions>
                <action type="Rewrite" url="?lon={R:1}&amp;lat={R:2}&amp;zoom={R:3}" />
            </rule>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.