3

我有一些乱七八糟的旧网址,例如...

http://www.example.com/bunch.of/unneeded/crap?opendocument&part=1

http://www.example.com/bunch.of/unneeded/crap?opendocument&part=2

...我想重定向到更新,更清洁的形式...

http://www.example.com/page.php/welcome

http://www.example.com/page.php/prices

我知道我可以通过简单的重定向将一个页面重定向到另一个页面,即

重定向 301 /bunch.of/unneeded/crap http://www.example.com/page.php

但是源页面没有改变,只是 GET vars。我不知道如何将重定向基于这些 GET 变量的值。有人可以帮忙吗!?我对旧的正则表达式相当方便,所以如果必须的话,我可以使用 mod-rewrite,但我不清楚重写 GET vars 的语法,我宁愿避免性能下降并使用更清洁的重定向指令。有办法吗?如果没有,任何人都可以告诉我正确的mod-rewrite语法吗?

干杯,

罗杰。

4

3 回答 3

5

由于 URL 查询中的参数可能具有任意顺序,因此您需要对每个要检查的参数或每个可能的排列使用一个RewriteCond指令。

RewriteCond以下是每个参数的指令示例:

RewriteCond %{QUERY_STRING} ^([^&]&)*opendocument(&|$)
RewriteCond %{QUERY_STRING} ^([^&]&)*part=1(&|$)
RewriteRule ^bunch\.of/unneeded/crap$ /page.php/welcome? [L,R=301]

RewriteCond %{QUERY_STRING} ^([^&]&)*opendocument(&|$)
RewriteCond %{QUERY_STRING} ^([^&]&)*part=2(&|$)
RewriteRule ^bunch\.of/unneeded/crap$ /page.php/prices? [L,R=301]

但正如你所看到的,这可能会变得一团糟。

因此,更好的方法可能是使用RewriteMap. 最简单的是带有键值的纯文本文件:

1 welcome
2 prices

要定义您的地图,请在您的服务器或虚拟主机配置中编写以下指令(在每个目录上下文中不允许此指令):

RewriteMap examplemap txt:/path/to/file/map.txt

那么你只需要一个规则:

RewriteCond %{QUERY_STRING} ^([^&]&)*opendocument(&|$)
RewriteCond %{QUERY_STRING} ^([^&]&)*part=([0-9]+)(&|$)
RewriteRule ^bunch\.of/unneeded/crap$ /page.php/%{examplemap:%2}? [L,R=301]
于 2009-05-16T08:27:01.973 回答
1
RewriteCond %{QUERY_STRING} option=com_content&task=view&id=70&Itemid=82
RewriteRule ^index.php http://www.example.com/business/banks/? [R=301,L]

这 ?将防止用户被发送到的 url 具有与原始页面相同的查询字符串。

于 2010-07-24T00:25:34.493 回答
0

总之,您可以使用RedirectMatch匹配完整 URL 的正则表达式,包括查询字符串。这将让您重新排列 URL 的各个部分,但如果您必须将“opendocument&part=1”转换为“welcome”(新 URL 与原始 URL 完全不同),您可能需要一个RewriteMap- 或者可能更好,只需将所有 URL 发送到page.php并解析 PHP 中的查询字符串。

编辑:如果只是您要重定向的几个 URL,您可能会写出单独的规则,例如

RedirectPermanent http://www.example.com/bunch.of/unneeded/crap?opendocument&part=1 http://www.example.com/page.php/welcome
RedirectPermanent http://www.example.com/bunch.of/unneeded/crap?opendocument&part=2 http://www.example.com/page.php/prices
于 2009-05-16T02:17:17.290 回答