htaccess的 - 的ErrorDocument VS重写规则 [英] .htaccess - ErrorDocument vs RewriteRule

查看:483
本文介绍了htaccess的 - 的ErrorDocument VS重写规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要为丢失的文件和无效的目录重定向到一个自定义404错误页面的请求。相信这可以通过两种方法来完成。

I want requests for missing files and invalid directories to redirect to a custom 404 error page. I believe this can be accomplished using 2 methods.

# redirect invalid requests and missing files to the not-found page  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^(.*)$ http://www.example.com/not-found.html [L]
# same idea but using error document  
ErrorDocument 404 /not-found.html

时的一个选项比其他更好?我什么时候会想使用一个相较于其他?

Is one option better than the other? When would I want to use one vs the other?

推荐答案

重写规则将不会返回正确的状态code。这意味着浏览器将看到您的自定义错误页,但响应code仍将是 200 ,然后在浏览器(或任何客户端与服务器通话)会认为特定的坏请求实际上是一个很好的请求。

The rewrite rule will not return the proper status code. Meaning that a browser will see your custom error page but the response code will still be 200, and the browser (or whatever client is talking to the server) will think that the specific bad request is actually a good request.

的ErrorDocument 账单会显示自定义错误页,但也将返回 404 响应code,让浏览器(或任何客户)知道是什么要求在服务器上未找到。

The ErrorDocument statement will show the custom error page but will also return a 404 response code, letting the browser (or whatever client) know that what it requested was not found on the server.

需要注意的是mod_rewrite的有一个研究标志,你可以给一个回应code到:

Note that mod_rewrite has an R flag that you can give a response code to:

RewriteRule ^(.*)$ /not-found.html [L,R=404]

这将返回一个 404 响应code,但则不会显示的的 /not-found.html 页。它的将会的,如果你使用的ErrorDocument 语句显示自定义错误文档:

This will return a 404 response code but it will not display the /not-found.html page. It will display the custom error document if you use the ErrorDocument statement:

RewriteRule ^(.*)$ - [L,R=404]
ErrorDocument 404 /not-found.html

重写规则将迫使404,它的目标基本上是忽略,那么的ErrorDocument 确保自定义错误页被端上来,以及适当的 404 响应。

The rewrite rule will force a 404, its target is essentially ignored, then the ErrorDocument makes sure the custom error page is served up as well as the proper 404 response.

所以,你要的ErrorDocument ,pretty的多少始终。另外要注意的是,mod_rewrite的是在URI不同的地方文件映射管道比的ErrorDocument 。如果你依靠的mod_rewrite来决定什么应该是404,什么不应该,它可能会影响其他模块中可能出现后(比如mod_proxy的,例如)。

So you want ErrorDocument, pretty much always. The other thing to note is that mod_rewrite is in a different place in the URI to File mapping pipeline than ErrorDocument. If you're relying on mod_rewrite to determine what should be 404 and what shouldn't, it's likely to be affected by other modules that may come after (like mod_proxy, for example).

需要明确的是,在的ErrorDocument 时的的网址到文件的映射管道的最后,资源未找到(即一个 404 ),然后将自定义错误文档声明中, ErrorDoucment 404 ,提供默认的最高自定义错误页来代替。当你使用mod_rewrite就像你在你的问题做什么,完全绕开管道到达自然方式的 404

To be clear, the ErrorDocument is used after the URL-to-file mapping pipeline concludes with a "resource not found" (i.e. a 404), then the custom error document statement, ErrorDoucment 404, serves up a custom error page instead of the default one. When you use mod_rewrite like you do in your question, it completely circumvents the natural way the pipeline arrives at a 404.

这篇关于htaccess的 - 的ErrorDocument VS重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆