翻译apache重写规则到IIS web.config

翻译apache重写规则到IIS web.config

问题描述:

我想把这个apache重写规则翻译成web.config规则,但我无法让它工作。翻译apache重写规则到IIS web.config

基本上,它会检查用户代理和代理重定向到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] 

这是我到目前为止所。但是,我无法弄清楚如何捕捉url querystring参数。基本上文本字符串后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> 
+0

你是自己编写规则,还是遵循这里的说明:http://www.iis.net/learn/extensions/url-rewrite-module/importing-apache-modrewrite-rules – astaykov 2014-09-05 09:41:06

+0

我正在写作我自己的...我只有安装了Visual Studio的IIS Express,并且找不到导入工具。 – Josef 2014-09-05 09:42:48

不是一个答案,而是一个起点。在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这当然不是一种模式,并且是您的规则不起作用的根本原因。

+0

非常感谢!这对我来说至少是一个非常好的起点。我会在今天晚些时候尝试。我自己的例子是我刚刚尝试基于各种在线示例拼图在一起的东西。 – Josef 2014-09-05 09:50:54

+0

注意'match'元素的'url'属性,这对工作起着非常重要的作用,正如你可能猜到的那样... – astaykov 2014-09-05 09:52:10

+0

是的绝对 – Josef 2014-09-05 09:54:56