1. 程式人生 > >URL Rewrite 導致404,500錯誤

URL Rewrite 導致404,500錯誤

write microsoft des tom -m request diag 自己 status

404 file not found,那就是rewrite的rules配的有問題,這裏有篇官方文檔,讀完了就不會犯這種錯啦。

https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference

500 server internal error,這個其實一般用ASP.NET才會遇到,尤其是MVC。。因為@Html.ActionLink之類的函數,在渲染的時候會和url rewrite沖突,報錯為:

Cannot use a leading .. to exit above the top directory.

解決方法也很簡單,把類似的函數用html重寫下就行了,比如

<li>@Html.ActionLink("Home", "Index", "Home")</li>

=>

<a href="/Home/Index" class="navbar-brand">Home</a>

還有種情況就是rewrite成功,返回了200,但是卻不是自己想要的結果,如果不能正向找到問題,那就把這段配置加到system.webServer節點中去,抓去所有返回200-299的訪問,然後訪問下網頁,再看看生成W3SVC的日誌。日誌生成後,記得把這段配置刪了,不然會非常卡,原因你懂的。

<tracing>
  <traceFailedRequests>
    <remove path="*" />
    <add path="*">
      <traceAreas>
        <add provider="ASP" verbosity="Verbose" />
        <add provider="ASPNET" areas="Infrastructure,Module,Page,AppServices" verbosity="Verbose" />
        <
add provider="ISAPI Extension" verbosity="Verbose" /> <add provider="WWW Server" areas="Rewrite,RequestRouting,Authentication,Security,Filter,StaticFile,CGI,Compression,Cache,RequestNotifications,Module,FastCGI" verbosity="Verbose" /> </traceAreas> <failureDefinitions statusCodes="200-299" /> </add> </traceFailedRequests> </tracing>

下面再來一個抓訪問慢的請求發生時的dump文件的配置

<tracing>
     <traceFailedRequests>
       <remove path="*" />
       <add path="*" customActionExe="d:\home\site\Diagnostics\procdump.exe" customActionParams="-ma -accepteula %1% d:\home\site\Diagnostics\w3wp_PID_%1%_" customActionTriggerLimit="5"> 
             <traceAreas>
                   <add provider="ASP" verbosity="Verbose" />
                   <add provider="ASPNET" areas="Infrastructure,Module,Page,AppServices" verbosity="Verbose" />
                   <add provider="ISAPI Extension" verbosity="Verbose" />
                   <add provider="WWW Server" areas="Authentication,Security,Filter,StaticFile,CGI,Compression,Cache,RequestNotifications,Module,FastCGI" verbosity="Verbose" />
             </traceAreas>
             <failureDefinitions timeTaken="00:00:30"  />
       </add>
     </traceFailedRequests></tracing>

@Html.ActionLink

URL Rewrite 導致404,500錯誤