简体   繁体   中英

optional parameters for redirect rule iis 7 url rewriting

How can I do a Redirect Rule that transforms this URL:

http://localhost/app/Company/HomePage.aspx?COMPANYID=2&FORCESHOWCASE=TRUE&THEMEPREVIEW=3

to this one:

http://localhost/app/2/showcase/preview/3

But the trick here is that all the parameters are optional except the COMPANYID, so the url can be:

http://localhost/app/2/preview/3

or:

http://localhost/app/2/showcase

I have not been able to find the right redirect rule to match this pattern. I've tried the following:

<rule name="RedirectUserFriendlyURLCompany" stopProcessing="true">
                    <match url="^Company/HomePage\.aspx$" />
                    <conditions>
                        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                        <add input="{QUERY_STRING}" pattern="^COMPANYID=([^=&amp;]+)(&amp;FORCESHOWCASE=([^=&amp;]+))?(&amp;THEMEPREVIEW=([^=&amp;]+))?$" />
                    </conditions>
                    <action type="Redirect" url="{C:1}" appendQueryString="false" />
                </rule>
                <rule name="RewriteUserFriendlyURLCompany" stopProcessing="true">
                    <match url="^([A-Za-z0-9]+)/?([A-Za-z0-9]*)(/preview/([A-Za-z0-9]*))?/?$" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="Company/HomePage.aspx?COMPANYID={R:1}&amp;FORCESHOWCASE={R:2}&amp;THEMEPREVIEW={R:4}" />
                </rule>

The Rewrite rule recognizes if I put some parameters or all of them and works great, if I try with any of the urls from above it rewrites it to the right url with the right parameters, but the problem is with the redirect rule, it doesn't work at all, if I put in the browser:

http://localhost/app/Company/HomePage.aspx?COMPANYID=2&FORCESHOWCASE=TRUE&THEMEPREVIEW=3

it doesn't redirect to the correct page and I dont know what to do, please help! Sorry for my English and thanks in advance

As nobody answered my question, it seems that theres no "easy" answer.

The way I make this work was changing my rules, using session variables instead of querystring

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM