0

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

第 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 到基于别名的逐页重定向,同时将三个站点/域合并到一个站点中。

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

4

4 回答 4

3

Redirect指令仅适用于URL 路径,但不适用于 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]

正如评论中已经说过的,您可以将重写映射用于 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]
于 2009-09-14T13:12:07.827 回答
1

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;
?>
于 2009-09-19T08:27:29.060 回答
0

只需添加更多信息

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

Redirect 指令通过要求客户端在新位置重新获取资源来将旧 URL 映射到新 URL。

旧的 URL 路径是以斜杠开头的区分大小写(% 解码)的路径。不允许使用相对路径。新 URL 应该是一个以方案和主机名开头的绝对 URL。例子:

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

这样就可以了:

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

希望有帮助

于 2009-09-19T08:47:53.653 回答
-1

在 httpd.conf 中:

重定向永久 URL1 URL2

于 2009-09-14T13:10:19.110 回答