2012-07-16 104 views
1

我正在將一些Sharepoint站點從一個服務器場遷移到另一個服務器場。這是一個更復雜一點,但爲了簡單起見...使用IIS URL重寫模塊修改查詢字符串

我想維護的是人們對這些網站,文檔等的舊網址和IIS URL重寫模塊似乎是一種很好的方式來走。

這裏的結構是什麼樣的想法:

_______________________   _______________________ 
|oldfarm.company.com**|   |newfarm.company.com**| 
|oldsitecollection** |   |newsitecollection** | 
|subsitename   |   |subsitename   | 
|...     |   |...     | 
|_____________________|   |_____________________| 

** =的變化,一切依舊,URLwise。

在「newfarm」我已經擴展了Web應用程序,以迴應「oldfarm.company.com」,而Web應用程序具有URL重定向規則重定向http://oldfarm.company.com/oldsitecollection/ ...到... http://newfarm.company.com/newsitecollection/

這對於我正在嘗試做的絕大多數工作很有用。

我很困難的是重寫QUERYSTRING值。 Sharepoint的Office文檔查看器包含QUERYSTRING中的路徑信息,這就是我需要改變的。

這裏是一個原始URL樣本: http://oldfarm.company.com/oldsitecollection/subsitename/_layouts/WordViewer.aspx?id=/oldsitecollection/subsitename/doclib/doc.docx&Source=http%3A%2F%2Foldfarm%2Ecompany%2Ecom%2Foldsitecollection%2Fsubsitename%2Fdoclib%2FForms%2FAllItems%2Easpx&DefaultItemOpen=1

這裏是重定向後的URL(並在我卡): http://newfarm.company.com/newsitecollection/subsitename/_layouts/WordViewer.aspx?id=/oldsitecollection/subsitename/doclib/doc.docx&Source=http%3A%2F%2Foldfarm%2Ecompany%2Ecom%2Foldsitecollection%2Fsubsitename%2Fdoclib%2FForms%2FAllItems%2Easpx&DefaultItemOpen=1

以下是我所需要的URL看起來像: http://newfarm.company.com/newsitecollection/subsitename/_layouts/WordViewer.aspx?id=/newsitecollection/subsitename/doclib/doc.docx&Source=http%3A%2F%2Fnewfarm%2Ecompany%2Ecom%2Fnewsitecollection%2Fsubsitename%2Fdoclib%2FForms%2FAllItems%2Easpx&DefaultItemOpen=1

我嘗試過使用重寫映射,因爲這些不是動態替換,但我無法讓它們修改QUERYSTRING。

這裏是我工作的重寫規則的例子:

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

回答

1

回答我的問題,什麼工作對我來說是創造我自己的custom rewrite provider

我創建了一個簡單的查找/替換供應商,看起來像這樣的供應商:

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

嗨,我在面對問題類似的情況 - 你能否請回答 - http://stackoverflow.com/questions/34506551/reading-cookie-value-using-url-rewrite-for-iis-wizard-unable-to-validate-at – codetoshare 2015-12-30 04:38:00