Translating apache rewrite rules to IIS web.config

2019-06-08 06:40发布

I am trying to translate this apache rewrite rule into web.config rules but I can't get it to work.

Basically it checks the user agent and redirect the agent to the url provided

# allow social media crawlers to work by redirecting them to a server-rendered static      version on the page
RewriteCond %{HTTP_USER_AGENT (facebookexternalhit/[09]|Twitterbot|Pinterest|Google.*snippet)
RewriteRule qs/(\d*)$ http://sitetocrawl.com/doc?id=$1 [P]

This is what I have so far. However, I can't figure out how to catch the url querystring parameter. Basically the text string after http://example.com/qs/parameter

<rule name="Social Rewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url="urltomatchpattern" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
  <add input="{HTTP_USER_AGENT}" pattern="facebookexternalhit/[0-9]|Twitterbot|Pinterest|Google.*snippet" />
 </conditions>
 <action type="Redirect" url="http://sitetocrawl.com/doc?parameter" appendQueryString="true" redirectType="Found" />
</rule>

EDIT:

I tried with many variants of simpler rules, like redirect/rewrite when a specific user agent requests the site(in my case, the facebook crawler). But I can't even get those rules to work. I am debugging using the Facebook OG debugger

  <rule name="Rule1" stopProcessing="true">        
      <match url=".*" /> 
      <conditions> 
        <add input="{HTTP_USER_AGENT}" pattern="facebookexternalhit/1.1|Facebot" /> 
      </conditions> 
      <action type="Redirect" url="new url here" />       
  </rule>   

1条回答
何必那么认真
2楼-- · 2019-06-08 07:24

Not an answer but a starting point. The IIS Manager (IIS 8 on Windows 8.1) translates your apache mod_rewrite rules into this slightly different configuration:

<rewrite>
  <rules>
    <rule name="Imported Rule 1" stopProcessing="true">
      <match url="qs/(\d*)$" ignoreCase="false" />
      <conditions>
        <add input="%{HTTP_USER_AGENT" pattern="(facebookexternalhit/[09]|Twitterbot|Pinterest|Google.*snippet)" ignoreCase="false" />
      </conditions>
      <action type="Rewrite" url="http://sitetocrawl.com/doc?id={R:1}" appendQueryString="false" />
    </rule>
  </rules>
</rewrite>

I see that it is rewrite instead of redirect, but please check if this would work for your scenario. And if it works, you may begin changing it until reaching desired result.

And now I see that Your main URL Matching pattern is simply urlmatchpattern which of course is not a pattern and is the root cause for your rules to not work.

查看更多
登录 后发表回答