使用IIS URL重写模块修改查询字符串
我正在将一些Sharepoint网站从一个服务器场迁移到另一个服务器场。这有点复杂,但为了简单起见 我想维护的是人们对这些站点、文档等的旧URL,而IIS URL重写模块似乎是一个不错的选择 以下是结构的概念:使用IIS URL重写模块修改查询字符串,iis,sharepoint-2010,url-rewriting,Iis,Sharepoint 2010,Url Rewriting,我正在将一些Sharepoint网站从一个服务器场迁移到另一个服务器场。这有点复杂,但为了简单起见 我想维护的是人们对这些站点、文档等的旧URL,而IIS URL重写模块似乎是一个不错的选择 以下是结构的概念: _______________________ _______________________ |oldfarm.company.com**| |newfarm.company.com**| |oldsitecollection** |
_______________________ _______________________
|oldfarm.company.com**| |newfarm.company.com**|
|oldsitecollection** | |newsitecollection** |
|subsitename | |subsitename |
|... | |... |
|_____________________| |_____________________|
**=改变,其他一切保持不变
在“newfarm”上,我扩展了web应用程序以响应“oldfarm.company.com”,该web应用程序有一个URL重定向规则,用于重定向。。。到
这对我所做的绝大多数工作都很有效
我遇到的困难是重写查询字符串值。Sharepoint的Office文档查看器在查询字符串中包含路径信息,这就是我需要更改的内容
以下是原始URL示例:
以下是重定向后的URL(以及我被卡住的地方):
以下是我需要的URL外观:
我尝试过使用重写映射,因为它们不是动态替换,但我无法让它们修改查询字符串
下面是我正在研究的重写规则的一个示例:
<rewrite>
<rewriteMaps>
<rewriteMap name="WordViewer">
<add key="id=/oldsitecollection" value="id=/newsitecollection" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Rewrite rule1 for WordViewer">
<match url=".*WordViewer.aspx" />
<conditions>
<add input="{WordViewer:{QUERY_STRING}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
为了回答我自己的问题,对我来说最有效的是创建我自己的 我创建的提供程序是一个简单的查找/替换提供程序,如下所示:
public class FindReplaceProvider : IRewriteProvider, IProviderDescriptor
{
public string Find { get; private set; }
public string Replace { get; private set; }
public void Initialize(IDictionary<string, string> settings, IRewriteContext rewriteContext)
{
string tmpFind, tmpReplace;
if (!settings.TryGetValue("Find", out tmpFind) || string.IsNullOrEmpty(tmpFind))
throw new ArgumentException("FindReplaceProvider setting 'Find' is required and cannot be empty");
if (!settings.TryGetValue("Replace", out tmpReplace))
throw new ArgumentException("FindReplaceProvider setting 'Replace' is required and cannot be null");
if (!string.IsNullOrEmpty(tmpFind))
Find = tmpFind;
else
throw new ArgumentException("FindReplaceProvider parameter 'Find' cannot be empty");
if (!string.IsNullOrEmpty(tmpReplace))
Replace = tmpReplace;
else
Replace = String.Empty;
}
public string Rewrite(string value)
{
return Regex.Replace(value, Find, Replace, RegexOptions.IgnoreCase);
}
public IEnumerable<SettingDescriptor> GetSettings()
{
yield return new SettingDescriptor("Find", "String to find");
yield return new SettingDescriptor("Replace", "String to replace");
}
}
<rewrite>
<providers>
<provider name="OfficeWebAppsReplaceId" type="MyFindReplaceProvider">
<settings>
<add key="Find" value="id=/oldsitecollection" />
<add key="Replace" value="id=/newsitecollection" />
</settings>
</provider>
<provider name="OfficeWebAppsReplaceSource" type="MyFindReplaceProvider">
<settings>
<add key="Find" value="http%3A%2F%2Foldfarm%2Ecompany%2Ecom%2Foldsitecollection%2" />
<add key="Replace" value="http%3A%2F%2Fnewfarm%2Ecompany%2Ecom%2Fnewsitecollection%2" />
</settings>
</provider>
</providers>
<rules>
<rule name="OfficeWebAppsQuerystringRedirect" stopProcessing="true">
<match url=".*(WordViewer.aspx|WordEditor.aspx|xlviewer.aspx|PowerPoint.aspx)$" />
<conditions logicalGrouping="MatchAny">
<add input="{QUERY_STRING}" pattern=".*id=/oldsitecollection.+" />
<add input="{QUERY_STRING}" pattern=".*Source=http%3A%2F%2Foldfarm%2Ecompany%2Ecom%2Foldsitecollection%2F.+" />
</conditions>
<action type="Redirect" url="{R:0}?{OfficeWebAppsReplaceId:{OfficeWebAppsReplaceSource:{C:0}}}" appendQueryString="false" redirectType="Temporary" />
</rule>
</rules>
</rewrite>
公共类FindReplaceProvider:IRewriteProvider、IProviderDescriptor
{
公共字符串Find{get;private set;}
公共字符串替换{get;private set;}
公共void初始化(IDictionary设置、IRewiteContext重写上下文)
{
字符串tmpFind,tmpReplace;
if(!settings.TryGetValue(“Find”,out tmpFind)| | string.IsNullOrEmpty(tmpFind))
抛出新ArgumentException(“FindReplaceProvider设置'Find'是必需的,不能为空”);
如果(!settings.TryGetValue(“Replace”,out tmpReplace))
抛出新ArgumentException(“FindReplaceProvider设置'Replace'是必需的,不能为null”);
如果(!string.IsNullOrEmpty(tmpFind))
Find=tmpFind;
其他的
抛出新ArgumentException(“FindReplaceProvider参数'Find'不能为空”);
如果(!string.IsNullOrEmpty(tmpReplace))
替换=tmpReplace;
其他的
Replace=String.Empty;
}
公共字符串重写(字符串值)
{
返回Regex.Replace(value、Find、Replace、RegexOptions.IgnoreCase);
}
公共IEnumerable GetSettings()
{
返回新的设置描述符(“查找”、“要查找的字符串”);
返回新的设置描述符(“替换”、“要替换的字符串”);
}
}
我的重写规则最终是这样的:
public class FindReplaceProvider : IRewriteProvider, IProviderDescriptor
{
public string Find { get; private set; }
public string Replace { get; private set; }
public void Initialize(IDictionary<string, string> settings, IRewriteContext rewriteContext)
{
string tmpFind, tmpReplace;
if (!settings.TryGetValue("Find", out tmpFind) || string.IsNullOrEmpty(tmpFind))
throw new ArgumentException("FindReplaceProvider setting 'Find' is required and cannot be empty");
if (!settings.TryGetValue("Replace", out tmpReplace))
throw new ArgumentException("FindReplaceProvider setting 'Replace' is required and cannot be null");
if (!string.IsNullOrEmpty(tmpFind))
Find = tmpFind;
else
throw new ArgumentException("FindReplaceProvider parameter 'Find' cannot be empty");
if (!string.IsNullOrEmpty(tmpReplace))
Replace = tmpReplace;
else
Replace = String.Empty;
}
public string Rewrite(string value)
{
return Regex.Replace(value, Find, Replace, RegexOptions.IgnoreCase);
}
public IEnumerable<SettingDescriptor> GetSettings()
{
yield return new SettingDescriptor("Find", "String to find");
yield return new SettingDescriptor("Replace", "String to replace");
}
}
<rewrite>
<providers>
<provider name="OfficeWebAppsReplaceId" type="MyFindReplaceProvider">
<settings>
<add key="Find" value="id=/oldsitecollection" />
<add key="Replace" value="id=/newsitecollection" />
</settings>
</provider>
<provider name="OfficeWebAppsReplaceSource" type="MyFindReplaceProvider">
<settings>
<add key="Find" value="http%3A%2F%2Foldfarm%2Ecompany%2Ecom%2Foldsitecollection%2" />
<add key="Replace" value="http%3A%2F%2Fnewfarm%2Ecompany%2Ecom%2Fnewsitecollection%2" />
</settings>
</provider>
</providers>
<rules>
<rule name="OfficeWebAppsQuerystringRedirect" stopProcessing="true">
<match url=".*(WordViewer.aspx|WordEditor.aspx|xlviewer.aspx|PowerPoint.aspx)$" />
<conditions logicalGrouping="MatchAny">
<add input="{QUERY_STRING}" pattern=".*id=/oldsitecollection.+" />
<add input="{QUERY_STRING}" pattern=".*Source=http%3A%2F%2Foldfarm%2Ecompany%2Ecom%2Foldsitecollection%2F.+" />
</conditions>
<action type="Redirect" url="{R:0}?{OfficeWebAppsReplaceId:{OfficeWebAppsReplaceSource:{C:0}}}" appendQueryString="false" redirectType="Temporary" />
</rule>
</rules>
</rewrite>
为了回答我自己的问题,对我来说最有效的是创建我自己的 我创建的提供程序是一个简单的查找/替换提供程序,如下所示:
public class FindReplaceProvider : IRewriteProvider, IProviderDescriptor
{
public string Find { get; private set; }
public string Replace { get; private set; }
public void Initialize(IDictionary<string, string> settings, IRewriteContext rewriteContext)
{
string tmpFind, tmpReplace;
if (!settings.TryGetValue("Find", out tmpFind) || string.IsNullOrEmpty(tmpFind))
throw new ArgumentException("FindReplaceProvider setting 'Find' is required and cannot be empty");
if (!settings.TryGetValue("Replace", out tmpReplace))
throw new ArgumentException("FindReplaceProvider setting 'Replace' is required and cannot be null");
if (!string.IsNullOrEmpty(tmpFind))
Find = tmpFind;
else
throw new ArgumentException("FindReplaceProvider parameter 'Find' cannot be empty");
if (!string.IsNullOrEmpty(tmpReplace))
Replace = tmpReplace;
else
Replace = String.Empty;
}
public string Rewrite(string value)
{
return Regex.Replace(value, Find, Replace, RegexOptions.IgnoreCase);
}
public IEnumerable<SettingDescriptor> GetSettings()
{
yield return new SettingDescriptor("Find", "String to find");
yield return new SettingDescriptor("Replace", "String to replace");
}
}
<rewrite>
<providers>
<provider name="OfficeWebAppsReplaceId" type="MyFindReplaceProvider">
<settings>
<add key="Find" value="id=/oldsitecollection" />
<add key="Replace" value="id=/newsitecollection" />
</settings>
</provider>
<provider name="OfficeWebAppsReplaceSource" type="MyFindReplaceProvider">
<settings>
<add key="Find" value="http%3A%2F%2Foldfarm%2Ecompany%2Ecom%2Foldsitecollection%2" />
<add key="Replace" value="http%3A%2F%2Fnewfarm%2Ecompany%2Ecom%2Fnewsitecollection%2" />
</settings>
</provider>
</providers>
<rules>
<rule name="OfficeWebAppsQuerystringRedirect" stopProcessing="true">
<match url=".*(WordViewer.aspx|WordEditor.aspx|xlviewer.aspx|PowerPoint.aspx)$" />
<conditions logicalGrouping="MatchAny">
<add input="{QUERY_STRING}" pattern=".*id=/oldsitecollection.+" />
<add input="{QUERY_STRING}" pattern=".*Source=http%3A%2F%2Foldfarm%2Ecompany%2Ecom%2Foldsitecollection%2F.+" />
</conditions>
<action type="Redirect" url="{R:0}?{OfficeWebAppsReplaceId:{OfficeWebAppsReplaceSource:{C:0}}}" appendQueryString="false" redirectType="Temporary" />
</rule>
</rules>
</rewrite>
公共类FindReplaceProvider:IRewriteProvider、IProviderDescriptor
{
公共字符串Find{get;private set;}
公共字符串替换{get;private set;}
公共void初始化(IDictionary设置、IRewiteContext重写上下文)
{
字符串tmpFind,tmpReplace;
if(!settings.TryGetValue(“Find”,out tmpFind)| | string.IsNullOrEmpty(tmpFind))
抛出新ArgumentException(“FindReplaceProvider设置'Find'是必需的,不能为空”);
如果(!settings.TryGetValue(“Replace”,out tmpReplace))
抛出新ArgumentException(“FindReplaceProvider设置'Replace'是必需的,不能为null”);
如果(!string.IsNullOrEmpty(tmpFind))
Find=tmpFind;
其他的
抛出新ArgumentException(“FindReplaceProvider参数'Find'不能为空”);
如果(!string.IsNullOrEmpty(tmpReplace))
替换=tmpReplace;
其他的
Replace=String.Empty;
}
公共字符串重写(字符串值)
{
返回Regex.Replace(value、Find、Replace、RegexOptions.IgnoreCase);
}
公共IEnumerable GetSettings()
{
返回新的设置描述符(“查找”、“要查找的字符串”);
返回新的设置描述符(“替换”、“要替换的字符串”);
}
}
我的重写规则最终是这样的:
public class FindReplaceProvider : IRewriteProvider, IProviderDescriptor
{
public string Find { get; private set; }
public string Replace { get; private set; }
public void Initialize(IDictionary<string, string> settings, IRewriteContext rewriteContext)
{
string tmpFind, tmpReplace;
if (!settings.TryGetValue("Find", out tmpFind) || string.IsNullOrEmpty(tmpFind))
throw new ArgumentException("FindReplaceProvider setting 'Find' is required and cannot be empty");
if (!settings.TryGetValue("Replace", out tmpReplace))
throw new ArgumentException("FindReplaceProvider setting 'Replace' is required and cannot be null");
if (!string.IsNullOrEmpty(tmpFind))
Find = tmpFind;
else
throw new ArgumentException("FindReplaceProvider parameter 'Find' cannot be empty");
if (!string.IsNullOrEmpty(tmpReplace))
Replace = tmpReplace;
else
Replace = String.Empty;
}
public string Rewrite(string value)
{
return Regex.Replace(value, Find, Replace, RegexOptions.IgnoreCase);
}
public IEnumerable<SettingDescriptor> GetSettings()
{
yield return new SettingDescriptor("Find", "String to find");
yield return new SettingDescriptor("Replace", "String to replace");
}
}
<rewrite>
<providers>
<provider name="OfficeWebAppsReplaceId" type="MyFindReplaceProvider">
<settings>
<add key="Find" value="id=/oldsitecollection" />
<add key="Replace" value="id=/newsitecollection" />
</settings>
</provider>
<provider name="OfficeWebAppsReplaceSource" type="MyFindReplaceProvider">
<settings>
<add key="Find" value="http%3A%2F%2Foldfarm%2Ecompany%2Ecom%2Foldsitecollection%2" />
<add key="Replace" value="http%3A%2F%2Fnewfarm%2Ecompany%2Ecom%2Fnewsitecollection%2" />
</settings>
</provider>
</providers>
<rules>
<rule name="OfficeWebAppsQuerystringRedirect" stopProcessing="true">
<match url=".*(WordViewer.aspx|WordEditor.aspx|xlviewer.aspx|PowerPoint.aspx)$" />
<conditions logicalGrouping="MatchAny">
<add input="{QUERY_STRING}" pattern=".*id=/oldsitecollection.+" />
<add input="{QUERY_STRING}" pattern=".*Source=http%3A%2F%2Foldfarm%2Ecompany%2Ecom%2Foldsitecollection%2F.+" />
</conditions>
<action type="Redirect" url="{R:0}?{OfficeWebAppsReplaceId:{OfficeWebAppsReplaceSource:{C:0}}}" appendQueryString="false" redirectType="Temporary" />
</rule>
</rules>
</rewrite>
Hi,我在类似情况下面临问题-您能回答吗-Hi,我在类似情况下面临问题-您能回答吗-