17

In IIS7 URL Rewrite module, can I specify in a redirect rule to not apply to http-post requests? I am using the templates provided by Microsoft to lowercase all urls and to append a trailing slash. However I have a AJAX post requests that don't meet this specification but they break we they are rewritten as 301s. I am not worried about POST requests for SEO so I would prefer if I could just specify in the rule to ignore it. Below are my rules:

            <rule name="AddTrailingSlashRule" stopProcessing="true">
                <match url="(.*[^/])$" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                </conditions>
                <action type="Redirect" url="{R:1}/" />
            </rule>
            <rule name="LowerCaseRule" stopProcessing="true">
                <match url="[A-Z]" ignoreCase="false" />
                <action type="Redirect" url="{ToLower:{URL}}" />
            </rule>
Blegger
  • 4,272
  • 4
  • 30
  • 36

2 Answers2

34

You have access to that in the {REQUEST_METHOD} variable under the conditions.

<add input="{REQUEST_METHOD}" matchType="Pattern" pattern="POST" ignoreCase="true" negate="true" />
patridge
  • 26,385
  • 18
  • 89
  • 135
  • 3
    I only found that by stumbling around in the rule system in IIS. The "Add Condition" window offers an intellisense experience on the input field when you type the first `{`. – patridge May 13 '11 at 16:09
  • 2
    Thanks for this answer. It was a big help to me, though I discovered you need to specify negate="true" in this condition so that the rewrite rule applies to things that are not REQUEST_METHOD = POST. – JamieGaines Jun 28 '12 at 14:35
  • Great catch, @thinkzig! I definitely have that `negate` in place where I am using this; I must have lost it in the shuffle to SO. I've corrected the answer now. – patridge Jul 02 '12 at 17:19
0

We've had the same problem as the OP a while back, and then applied patridge's solution, which worked fine until we noticed some REST DELETE calls would fail. Turned out to be the trailing slash redirect making GETs out of the DELETE requests.

So I modified the solution to make the redirect rule to apply only to GET requests.

<add input="{REQUEST_METHOD}" matchType="Pattern" pattern="GET" ignoreCase="true" />
Johan B
  • 890
  • 3
  • 23
  • 39