How to redirect 404 error page with 404 response code to a custom page using htaccess

别等时光非礼了梦想. 提交于 2019-12-13 07:08:21

问题


I want to REDIRECT 404 error page to custom 404 page. For example, if http://example.com/abc.html causes 404 error, I want redirect the page to http://example.com/404.html with 404 http response code. It is possible?


回答1:


So far there is NO OPTION to redirect 404 error page to a custom 404 page with 404 http response code. To get 404 http response code, you will need to display custom 404 error page content without redirection like following code:

ErrorDocument 404 /404.html

And if you use following code, the 404 error page will be redirected to the custom 404 page with 302 http response code

ErrorDocument 404 http://www.yourdomain.com/404.html



回答2:


Since you are using Apache, you can use the ErrorDocument directive (docs).

You are encouraged to use an internal rewrite for this, so the client receives the correct statuscode for the request (404). If you make it redirect to this page, the client will receive a 301 or 302 header instead for the non-existing page, which can confuse automated webservices, such as webcrawlers.

To make it internally rewrite:

ErrorDocument 404 /404.html

To make it externally redirect

ErrorDocument 404 http://yoursite.com/404.html


来源:https://stackoverflow.com/questions/20883365/how-to-redirect-404-error-page-with-404-response-code-to-a-custom-page-using-hta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!