翻译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>
答
不是一个答案,而是一个起点。在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
这当然不是一种模式,并且是您的规则不起作用的根本原因。
你是自己编写规则,还是遵循这里的说明:http://www.iis.net/learn/extensions/url-rewrite-module/importing-apache-modrewrite-rules – astaykov 2014-09-05 09:41:06
我正在写作我自己的...我只有安装了Visual Studio的IIS Express,并且找不到导入工具。 – Josef 2014-09-05 09:42:48