所有页面的 Htaccess“410 GONE”并重定向到 index.php

Htaccess "410 GONE" for all pages and redirect to index.php

我想让 google 和其他机器人永远删除我的旧网页 (410 GONE) 并重定向到主页 index.php,其中包含带有 link 的简单文本tonew 域 "The site is moved to new domain www.new_domain.com"

因此,请求所有已删除的页面,例如:

www.old_domain.com/about.php
www.old_domain.com/view.php?id=123

应发送至 www.old_domain.com/index.php,将 return 代码 410 发送至 Google,即“除 index.php 之外的所有页面都将永远消失”

实现此目标的最佳方法是什么?

  1. 使用 PHP 代码 header( "HTTP/1.1 410 Gone" ); 插入 index.php 文件
    <!DOCTYPE html>
    <?PHP    
    header( "HTTP/1.1 410 Gone" );
    ?>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="googlebot" content="noindex, nofollow" />
    <meta name="robots" content="noindex, nofollow" />
    <title>www.old_domain.com</title>
    </head>
    <body>
    The site is moved to <a href="https://www.new_domain.com/" 
    title="https://www.new_domain.com/">https://www.new_domain.com/</a>
    </body>
    </html>
  1. 或者htaccess方法:
RewriteEngine On
RewriteBase /
RewriteRule ^robots.txt - [L]
RewriteRule ^(.*)$ https://www.old_domain.com.com/? [G,L]

这似乎不起作用,因为重定向到 Apache 410 错误页面并且访问者看不到新站点从 index.php 移动到哪里。

注意:我不想使用 301 重定向到新域。

你可以使用这个:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule !index\.php - [R=410,L]

只需将上述条件模式中的 olddomain.com 替换为您的域名即可。

“410 Gone”状态是错误状态,不是重定向。所有重定向状态都在 300 年代。您不能同时提供 410 状态和重定向。 HTTP 规范不允许这样做。

您可以改为将自定义页面设置为“已消失”状态:

ErrorDocument 410 /410-gone.html

并将您的消息放入该文件中。然后 Apache 将为所有“消失”的请求提供该消息,并且不需要重定向。