2015-11-06 24 views
1

我試圖重寫URL以使其搜索引擎友好IIS甚至重寫改寫圖像/ CSS/JS - 結果404沒有發現

www.mydomain.com/qa/213/who-am-i

重寫作爲

www.mydomain.com/qa/?qa=213/who-am-i

的下面塊的工作方式,但問題是,JS/CSS /圖像網址內該頁面也被重寫。因此,該頁面會查找實際不存在的文件,如www.mydomain.com/qa/213/who-am-i/jquery.js。所以頁面加載,但沒有一個CSS,。JS和圖像工作。

<rule name="CleanRouting" stopProcessing="true"> 
      <match url="^qa/(.*)/(.*)$" ignoreCase="true" /> 
      <conditions> 
      <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" /> 
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" /> 
      </conditions> 
      <action type="Rewrite" url="qa/?qa={R:1}/{R:2}&amp;{QUERY_STRING}" appendQueryString="false" /> 
     </rule> 

請告訴我如何解決這個問題。我正在使用Asp.Net MVC(如果它很重要)。

回答

1

經過幾個小時&天后,我終於找到了正確的IIS重寫規則,用於繞過images/css/js等文件,以便頁面正確顯示。

以下重寫規則必須添加到您的ASP.Net MVC項目的Web.Config。

<!--Rewrite all paths containing a period like www.conceptworld.com/qa/453/who-am-i/qa-images/search.png to www.conceptworld.com/qa/qa-images/search.png --> 

<rule name="RewriteFileUrls" stopProcessing="true"> 
      <match url="^qa\/([^\/]*)\/([^\/]*)\/(.*[\.].*)$" ignoreCase="true" /> 
      <conditions> 
      <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" /> 
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" /> 
      </conditions> 
      <action type="Rewrite" url="qa/{R:2}/{R:3}" appendQueryString="false" /> 
     </rule> 

我使用上面的規則加上低於2個規則爲Question2Answer問題回答基於PHP的解決方案類似的StackOverflow。我在IIS + Asp.Net MVC的Windows系統上安裝了Question2Answer

<!--Rewrites urls like www.conceptworld.com/qa/tags --> 

<rule name="RewriteSingleLevelUrls" stopProcessing="true"> 
      <match url="^qa\/([^\/]*)$" ignoreCase="true" /> 
      <conditions> 
      <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" /> 
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" /> 
      </conditions> 
      <action type="Rewrite" url="qa/?qa={R:1}&amp;{QUERY_STRING}" appendQueryString="false" /> 
     </rule> 

<!--Rewrites urls like www.conceptworld.com/qa/56/who-am-i --> 
     <rule name="RewriteTwoLevelUrls" stopProcessing="true"> 
      <match url="^qa\/([^\/]*\/[^\/]*)$" ignoreCase="true" /> 
      <conditions> 
      <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" /> 
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" /> 
      </conditions> 
      <action type="Rewrite" url="qa/?qa={R:1}&amp;{QUERY_STRING}" appendQueryString="false" /> 
     </rule> 

只要設置上述規則是不夠的。最重要的是將您的基本網址設置爲Question2Answer。我幾乎放棄了,並且認爲當它安裝在Windows(IIS + Asp.Net MVC)上時,在Question2Answer中不可能有非常友好的SEO,直到我發現這個設置。由於Question2Answer開發商:)

進入管理/佈局與設置基本網址,如下所示:

Question2Answer - Admin/Layout Page

現在,你都設置與IIS Asp.Net MVC一起運行Question2Answer。