apache .htaccess 将所有流量重定向到一页 (410 Gone)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1975904/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:32:17  来源:igfitidea点击:

.htaccess to Redirect All Traffic to One Page (410 Gone)

apache.htaccesshttpredirecthttp-status-code-410

提问by Ben Dyer

We have a client that is closing its doors. We want to redirect all traffic that goes to their domain to a new page index.html with a few images in the _img subdirectory. (The page explains what happened, what current customers can expect with their current orders, etc.)

我们有一个客户正在关门大吉。我们希望将所有进入其域的流量重定向到一个新页面 index.html,并在 _img 子目录中添加一些图像。(该页面解释了发生的事情、当前客户对当前订单的期望等)

I've read about possibly using HTTP 410 Gone as the best way to technically explain to bots, etc. that the site is not there, isn't coming back and isn't providing a forwarding address. What would be the best way to do this in an .htaccess file, and direct users to the new index.html?

我已经读到可能使用 HTTP 410 Gone 作为从技术上向机器人等解释该站点不在那里、不会回来并且不提供转发地址的最佳方式。在 .htaccess 文件中执行此操作并将用户定向到新的 index.html 的最佳方法是什么?

回答by Gumbo

You can use mod_rewritefor this.

您可以为此使用mod_rewrite

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^index\.html$ index.html [L,R=410]

This rule will rewrite requests to non-existing files to index.htmland send the 410 status code along with the response. But this requires Apache 2 as R=4xxis only available since Apache 2.

此规则将对不存在的文件的请求重写为index.html并将 410 状态代码与响应一起发送。但这需要 Apache 2,因为R=4xx仅在 Apache 2 之后可用。

回答by Candy

You can simply use an .htaccess file like this:

您可以简单地使用 .htaccess 文件,如下所示:

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteCond %{REQUEST_URI} !.*index\.php
   RewriteRule (.*) /index.php
</IfModule>

回答by Gary Samad

This is much simpler:

这要简单得多:

RedirectMatch temp .* http://www.newdomain.com/newdestination.html

It redirects every single request to newdestination.html.

它将每个请求重定向到 newdestination.html。

Note that if you point to the same domain as the source, there will be an infinite loop and this will fail. This works great pointing to a new domain, though.

请注意,如果您指向与源相同的域,则会出现无限循环并且会失败。不过,这在指向一个新域时效果很好。