URL Rewrite 导致404,500错误

Posted nicklooo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了URL Rewrite 导致404,500错误相关的知识,希望对你有一定的参考价值。

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:homesiteDiagnosticsprocdump.exe" customActionParams="-ma -accepteula %1% d:homesiteDiagnosticsw3wp_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错误的主要内容,如果未能解决你的问题,请参考以下文章

.htaccess url rewrite在GoDaddy Shared Server上不起作用,返回了Apache 2.4.3 - 404错误

服务器内访问laravel框架 404错误(宝塔)

htaccess - RewriteRules 声明导致 404 错误

301 重写/重定向规则导致 500 内部服务器错误

Rails 4,Ajax 导致 404 和 500 错误

尝试访问损坏的图片 url 时抛出内部服务器错误 500 而不是 404