简体   繁体   English

将Apache重写规则转换为IIS web.config

[英]Translating apache rewrite rules to IIS web.config

I am trying to translate this apache rewrite rule into web.config rules but I can't get it to work. 我正在尝试将此apache重写规则转换为web.config规则,但无法正常工作。

Basically it checks the user agent and redirect the agent to the url provided 基本上,它检查用户代理并将代理重定向到提供的URL

# 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. 但是,我不知道如何捕获url querystring参数。 Basically the text string after http://example.com/qs/parameter 基本上是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). 我尝试了许多更简单规则的变体,例如当特定用户代理请求该站点(在我的情况下为Facebook搜寻器)时重定向/重写。 But I can't even get those rules to work. 但是我什至无法使这些规则生效。 I am debugging using the Facebook OG debugger 我正在使用Facebook OG调试器进行调试

  <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>   

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: IIS管理器(Windows 8.1上为IIS 8)将您的apache mod_rewrite规则转换为此稍有不同的配置:

<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. 现在,我看到您的主要URL匹配模式只是urlmatchpattern ,这当然不是一个模式,并且是您的规则不起作用的根本原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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