【问题标题】:Translating apache rewrite rules to IIS web.config将 apache 重写规则转换为 IIS web.config
【发布时间】:2014-10-29 06:26:06
【问题描述】:

我正在尝试将此 apache 重写规则转换为 web.config 规则,但我无法让它工作。

基本上它会检查用户代理并将代理重定向到提供的网址

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

这是我目前所拥有的。但是,我不知道如何捕获 url 查询字符串参数。基本上是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>

编辑:

我尝试了许多更简单规则的变体,例如在特定用户代理请求站点时重定向/重写(在我的例子中是 facebook 爬虫)。但我什至无法让这些规则发挥作用。我正在使用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>   

【问题讨论】:

标签: asp.net iis web-config rewrite


【解决方案1】:

不是一个答案,而是一个起点。 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>

我看到它是重写而不是重定向,但请检查这是否适用于您的场景。如果它有效,您可以开始更改它,直到达到所需的结果。

现在我看到您的主要 URL 匹配模式只是 urlmatchpattern,这当然不是一种模式,而是您的规则不起作用的根本原因。

【讨论】:

  • 非常感谢!这对我来说至少是一个很好的起点。今天晚些时候我会试试的。我自己的例子是我刚刚尝试根据网上的各种例子拼凑出来的。
  • 注意match 元素的url 属性,正如您可能猜到的那样,这对于工作非常重要...
  • 不,我无法让它工作。实际上,我尝试了 30 种不同的规则,这些简单的规则只是为了重定向来自特定 USER_AGENT 的任何传入请求,而无需任何参数。然后我用facebook OG调试器工具查看是否获取到规则提供的url,但没有成功奇怪。
  • 您好,%{HTTP_USER_AGENT} 中有错字
猜你喜欢
  • 2012-08-22
  • 2016-01-02
  • 2011-04-25
  • 2012-07-12
  • 2014-07-03
  • 1970-01-01
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多