2014-03-06 56 views
0

假設您在IIS 7上託管Wordpress,並且您想激活永久鏈接。縱觀抄本here,你會看到web.config文件(位於你的WordPress安裝的根目錄)這個例子的代碼在<system.webServer>標籤添加:WordPress的Wordpress永久鏈接與WCF服務使用服務工廠

<rewrite> 
    <rules> 
     <rule name="Main Rule" stopProcessing="true"> 
      <match url=".*" /> 
      <conditions logicalGrouping="MatchAll"> 
       <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
       <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
      </conditions> 
      <action type="Rewrite" url="index.php" /> 
     </rule> 
    </rules> 
</rewrite> 

一切都很好,和網址重寫規則的條件正確地忽略系統上存在的現有文件和文件夾。

然而,你可能會到situtation,像我一樣,在這裏你需要忽略每一個處於某個子文件夾中的URI,即使沒有文件系統中的相應文件。在我來說,我使用的<serviceActivations>面積在web.config中(在子目錄)的形式舉辦一系列WCF服務的子目錄/Services/

<serviceHostingEnvironment multipleSiteBindingsEnabled="true"> 
    <serviceActivations> 
    <add factory="System.ServiceModel.Activation.ServiceHostFactory" relativeAddress="./AccountService.svc" service="MyAssembly.Services.AccountService"/> 
    </serviceActivations> 
</serviceHostingEnvironment> 

導航到AccountService.svc會造成404未找到。

你如何處理這種情況?下面我發佈了我的具體解決方案,但還有其他方法可以實現這一點。此外,一般情況下的解決方案仍然是難以捉摸的,所以問題仍然存在,您通常如何處理這種情況?

回答

0

下面我提供了我的解決方案作爲一個完整的新手到URL重寫的世界。該解決方案特定於特定的文件夾。

解決我的問題是有必要的附加條件添加到重寫規則:

<rewrite> 
    <rules> 
     <rule name="Main Rule" stopProcessing="true"> 
      <match url=".*" /> 
      <conditions logicalGrouping="MatchAll"> 
       <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
       <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
       <add input="{REQUEST_URI}" pattern="^/Services/.*" negate="true" /> 
      </conditions> 
      <action type="Rewrite" url="index.php" /> 
     </rule> 
    </rules> 
</rewrite> 

在你的情況,你的替換文件夾名稱,在那裏我有「服務」就足夠了。

如果你想爲任何目錄或目錄層次結構做到這一點,例如,如果該目錄層次結構中存在,然後讓含有目錄層次穿過而不被重寫任何URL。我在此嘗試迄今圍繞着匹配其中包含的目錄層次結構中的URL部分,並使用一個反向引用,如果這個目錄存在檢測:

 <rule name="ignoreSubDirectoryURIs" stopProcessing="true"> 
      <match url="(.*)/.*\..*" ignoreCase="false" /> 
      <conditions> 
       <add input="{APPL_PHYSICAL_PATH}\{R:1}\" matchType="IsDirectory" /> 
      </conditions> 
      <action type="None" /> 
     </rule> 

此規則第一次添加,這樣,如果它是成功的另一個重寫規則不被解釋。

問題:雖然{APPL_PHYSICAL_PATH}是一個目錄,我還沒有能夠得到{APPL_PHYSICAL_PATH}和子目錄的任何組合來正確解析。另一個問題是嵌套目錄,其中{R:1}將返回dir1/dir2,而不是dir1 \ dir2,這可能會使這不適用於嵌套的情況。

如果讀取URL Rewrite Reference不完整