【问题标题】:Redirect pages from old to new site - one by one将页面从旧站点重定向到新站点 - 一个接一个
【发布时间】:2010-11-28 03:07:25
【问题描述】:

我正在尝试在一个新域上逐页重定向来自多个旧域的页面,即:

第 1 页:http://www.example.org/default.asp?id=1 重定向到 http://www.example2.org/newpage.html 第 2 页:http://www.example2.org/default.asp?id=2 重定向到 http://www.example.org/contact.html

...等等。每个旧页面都会重定向到特定的新页面。

我试图在 .htaccess 中写一些东西

Redirect 301 http://www.example.org/default.asp?id=1 h-tp://www.example.org/newpage.html
Redirect 301 http://www.example2.org/default.asp?id=2 h-tp://www.example.org/contact.html

但到目前为止还没有运气。 mod_alias 已启用...

我也尝试过像这样使用 RewriteCond 和 RewriteRule:

RewriteCond %{HTTP_HOST} example.org
RewriteRule default.asp?id=1 http://www.example.org/newpage.html [NC,L,R=301]
...

换句话说:我想进行从基于 id 到基于别名的逐页重定向,同时将三个站点/域合并到一个站点中。

我希望有一个有用的解决方案。提前致谢!

【问题讨论】:

    标签: .htaccess redirect seo rewrite


    【解决方案1】:

    Redirect 指令仅适用于 URL path,但不适用于 URL 的主机或查询。

    mod_rewrite 是可能的:

    RewriteCond %{HTTP_HOST} =example.org
    RewriteCond %{QUERY_STRING} =id=1
    RewriteRule ^default\.asp$ http://www.example.org/newpage.html [NC,L,R=301]
    

    正如在 cmets 中已经说过的,您可以使用 rewrite map 进行 ID 到别名的映射:

    1 foo-page
    2 bar-page
    3 baz-page
    …
    

    RewriteMap 声明(这里是一个简单的纯文本文件):

    RewriteMap id-to-alias txt:/absolute/file/system/path/to/id-to-alias.txt
    

    最后是应用程序:

    RewriteCond %{HTTP_HOST} =example.org
    RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=([0-9]+)&?([^&].*)?$
    RewriteCond ${id-to-alias:%3}&%1%4 ^([^&]+)&(.*)
    RewriteRule ^default\.asp$ http://www.example.org/%1.html?%2 [NC,L,R=301]
    

    这也应该保留剩余的查询。如果你不想这样:

    RewriteCond %{HTTP_HOST} =example.org
    RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=([0-9]+)&?([^&].*)?$
    RewriteCond ${id-to-alias:%3} .+
    RewriteRule ^default\.asp$ http://www.example.org/%0.html? [NC,L,R=301]
    

    【讨论】:

    • 所以基本上我需要每页两行?一个与 RewriteCond 匹配 id 一个与 RewriteRule 确保将用户发送到正确的页面?并以 [L] 结束?我终于走上了正确的道路吗?
    • 一般来说,是的。但是您可以使用重写映射来存储 ID 到别名的映射(请参阅 httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritemap)。
    • 为了其他人的帮助:RewriteMap 声明只能进入 httpd.conf - 不要像我刚才那样麻烦将其放入 .htaccess 中。
    【解决方案2】:

    mod_rewrite 的一个很好的替代方法是使用您最熟悉的任何语言并将逻辑放入您的脚本中。将您希望重定向到简单前端控制器的所有 URL 指向。

    RewriteRule ^default.asp$ /redirect.php [L]
    RewriteRule ^another/path/to_some_file.ext$ /redirect.php [L]
    

    然后,在redirect.php中:

    <?php
    if ($_SERVER['SCRIPT_URL'] == '/default.asp'){
        $map = array(
            1 => '/newpage.html',
            2 => '/contact.html'
        );
        if (isset($_GET['id'])){
            $id = $_GET['id'];
            if (isset($map[$id])){
                header('HTTP/1.1 301 Moved Permanently', true, 301);
                header('Location: http://'.$_SERVER['HTTP_HOST'].$map[$id]);
                exit;
            }
        }
    }
    // ... handle another/path/to_some_file.ext here, or other domains, or whatever ...
    
    // If we get here, it's an invalid URL
    header('HTTP/1.1 404 Not Found', true, 404);
    echo '<h1>HTTP/1.1 404 Not Found</h1>';
    echo '<p>The page you requested could not be found.</p>';
    exit;
    ?>
    

    【讨论】:

      【解决方案3】:

      只是添加更多信息

      OP 的重定向指令可能格式错误 - 来自相关的 apache 文档page

      重定向指令映射旧网址 通过要求客户进入一个新的 在新的地方重新获取资源 位置。

      旧的 URL 路径区分大小写 (%-decoded) 路径 以 a 开头 斜线。相对路径不是 允许。新的 URL 应该是 以方案开头的绝对 URL 和主机名。示例:

      Redirect /service http://foo2.bar.com/service
      

      这样就可以了:

      Redirect 301 /default.asp?id=1 http://www.example.org/newpage.html
      

      希望有帮助

      【讨论】:

      • 但正如引用的摘录所述,Redirect 仅适用于 URL 路径并将给定的 URL 路径作为要匹配的前缀。
      【解决方案4】:

      在 httpd.conf 中:

      重定向永久 URL1 URL2

      【讨论】:

      • 不幸的是,这种方法不可用,因为我无权访问 httpd.conf
      • 但我发现的所有使用重定向的示例都只能在同一主机上使用。
      猜你喜欢
      • 2013-10-25
      • 2016-06-07
      • 2017-07-25
      • 2017-01-08
      • 1970-01-01
      • 1970-01-01
      • 2019-07-03
      • 1970-01-01
      • 2011-06-03
      相关资源
      最近更新 更多