使用 apache 将 404 重定向到另一个域?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1189307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect 404 to another domain with apache?
提问by Jose Boveda
Hey guys, have a question regarding apache. I have a site that's been re-engineered, but I want to capture all the 'old' links that people may have bookmarked or come from search engines to the old site which is under a new domain name. How do I get apache to redirect only 404 not found to the old site?
大家好,有一个关于apache的问题。我有一个经过重新设计的网站,但我想捕获人们可能已添加书签或来自搜索引擎到位于新域名下的旧网站的所有“旧”链接。如何让 apache 仅将未找到的 404 重定向到旧站点?
TIA,
TIA,
J
J
回答by Gumbo
You should first decide what status code you want to send. Sending both a 404 status code and a redirect is not possible.
您应该首先决定要发送的状态代码。无法同时发送 404 状态代码和重定向。
But seth did already mention the right method, the ErrorDocumentdirective:
但是 seth 确实已经提到了正确的方法,即ErrorDocument指令:
# local path
ErrorDocument 404 /local/path/to/error/document
# external URI
ErrorDocument 404 http://uri.example/to/error/document
If you use a local path, the 404 status code is sent. If you use an absolute URI, a 302 status code (temporary redirect) is sent.
如果您使用本地路径,则会发送 404 状态代码。如果您使用绝对 URI,则会发送 302 状态代码(临时重定向)。
And if you want to send a 301 redirect:
如果您想发送 301 重定向:
Redirect 301 / http://new.example.com/
回答by workmad3
Your old domain should capture all responses and return a '301 moved permanently' response with the new domain in the 'Location' field of the header. A 404 means 'not found' and in this case it's not strictly true.
您的旧域应捕获所有响应并返回“301 永久移动”响应,并在标头的“位置”字段中使用新域。404 表示“未找到”,在这种情况下,它并非严格正确。
回答by chris
Another option, similar to that proposed by @seth is to add the handler so it points to a static html page which you can use to explain to the user what happen, and present them with options.
另一个选项,类似于@seth 提出的选项,是添加处理程序,使其指向一个静态 html 页面,您可以使用该页面向用户解释发生了什么,并为他们提供选项。
You can include a meta redirect so that if they don't do anything after a few seconds they're automatically redirected.
你可以包含一个元重定向,这样如果他们在几秒钟后没有做任何事情,他们就会被自动重定向。
Which option will work best is really up to you do decide.
哪个选项最有效取决于您自己决定。
回答by seth
You could set your 404 document to a CGI that redirects the user.
您可以将 404 文档设置为重定向用户的 CGI。
ErrorDocument 404 /cgi-bin/redirect-to-other.cgi

