June 13, 2007
最新版的Permalink Redirect 0.7.0 中集成了原永久链接定向到新永久链接的功能, 可imxl试用后告诉我说不行, 因为我一直也没有用到这个功能。 所以一直没有去仔细看看这代码。
昨天我想把我现在使用的永久链接改掉, 觉得现在的 %postname%-%post_id%.html 这样的架构实在不是很好, 太过麻烦。 而且 post_id 也可能会有变动; 最后决定去掉 %post_id% 采用 %postname%.html 的永久链接格式。 这样的话, 如果有人从搜索引擎先前的链接上过来就会出现无法找到这个页面的错误, 从而损失流量等等。 不过使用wordpress的朋友是很有福的, 应该有好多插件可以实现这个功能, 就我所知, 最著名的就是 Dean’s Permalinks Migration 了。 不过新版的 Permalink Redirect 0.7.0 也有了该功能, 所以我就不高兴再加一个插件了。 不过在试用后发现如 imxl 说的, 还真是不行。 还是打不了老的永久链接, 提示是页面不存在。
这个可奇怪了, Permalink Redirect 再差也不会连这个东西都不会做的吧? 肯定不会, 那会是什么呢? 把两个插件都下载过来, 一对比, 看出问题来了。
原来在 Permalink Redirect 0.7.0 中关于old permalinks 转换到新的 permalinks 是这么写的代码:
$rules = array_merge($rules,
$wp_rewrite->generate_rewrite_rule($oldstruct, false, false, false, true));
而在 Dean’s Permalinks Migration 中代码却是这么写的:
if ($dean_pm_config ['highpriority'] == true)
{
return array_merge($dean_pm_config['rewrite'],$rules);
}
else
{
return array_merge($rules, $dean_pm_config['rewrite']);
}
看似没有什么区别, 但这里要说一点, 在 Dean’s Permalinks Migration 插件中, $dean_pm_config ['highpriority'] 被定义成了 true, 所以 Dean’s Permalinks Migration 中该代码的执行应该是这一句:
return array_merge($dean_pm_config['rewrite'],$rules);
现在看出问题了吧, array_merge()函数中的两个参数刚好互换了一个位置。 这里申明一下, $rules 参数是wordpress带进来的, 另外一个参数才是内部私有并有所动作的。
既然找出了问题可能所在, 那就把语句改一改。 把 Permalink Redirect 中的语句改成这样就可以用了:
$rules = array_merge(
$wp_rewrite->generate_rewrite_rule($oldstruct, false, false, false, true),
$rules);
然后按照使用说明, 先在 Permalink Redirect 中设置 old Permalink, 然后再去 “选项->永久链接”页面设置新的永久链接格式, 就一切ok了。
怎么样? 成功了吗? 可以少一个插件了吧,:)
我最后把使用这种可以实现目的的格式查到的 return 值导出来了, 是这样的。
Array
(
[archives/([^/]+)-([0-9]+).html/trackback/?$] => index.php?name=$matches[1]&p=$matches[2]&tb=1
[archives/([^/]+)-([0-9]+).html(/[0-9]+)?/?$] => index.php?name=$matches[1]&p=$matches[2]&page=$matches[3]
[archives/[^/]+-[0-9]+.html/([^/]+)/?$] => index.php?attachment=$matches[1]
[archives/[^/]+-[0-9]+.html/([^/]+)/trackback/?$] => index.php?attachment=$matches[1]&tb=1
[archives/[^/]+-[0-9]+.html/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
[archives/[^/]+-[0-9]+.html/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
[archives/[^/]+-[0-9]+.html/attachment/([^/]+)/?$] => index.php?attachment=$matches[1]
[archives/[^/]+-[0-9]+.html/attachment/([^/]+)/trackback/?$] => index.php?attachment=$matches[1]&tb=1
[archives/[^/]+-[0-9]+.html/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
[archives/[^/]+-[0-9]+.html/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
[archives/([^/]+).html/trackback/?$] => index.php?name=$matches[1]&tb=1
[archives/([^/]+).html/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?name=$matches[1]&feed=$matches[2]
[archives/([^/]+).html/(feed|rdf|rss|rss2|atom)/?$] => index.php?name=$matches[1]&feed=$matches[2]
[archives/([^/]+).html/page/?([0-9]{1,})/?$] => index.php?name=$matches[1]&paged=$matches[2]
[archives/([^/]+).html(/[0-9]+)?/?$] => index.php?name=$matches[1]&page=$matches[2]
[archives/[^/]+.html/([^/]+)/?$] => index.php?attachment=$matches[1]
[archives/[^/]+.html/([^/]+)/trackback/?$] => index.php?attachment=$matches[1]&tb=1
[archives/[^/]+.html/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
[archives/[^/]+.html/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
[archives/[^/]+.html/attachment/([^/]+)/?$] => index.php?attachment=$matches[1]
[archives/[^/]+.html/attachment/([^/]+)/trackback/?$] => index.php?attachment=$matches[1]&tb=1
[archives/[^/]+.html/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
[archives/[^/]+.html/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
)
我把这输出的内容分成两部分, 分别用红色和蓝色表示。 红色的表示的是array_merge()函数的前半部分内容, 绿色的则是后半部分内容, 两者的=>之前的内容都具有相似性。 但只有红色格式中的内容才能让你如愿以偿, 具体的我就不分析了。
您喜欢本文吗?即刻订阅"偶爱偶家",精彩文章不再错过!现在就给我们留个话吗?
Random Posts


4 responses to "修正Permalink Redirect节省Dean’s Permalinks Migration"
你好强!我因为这个把两个都启用了的,对了,301重定向之后百度可能反映不对,我的重定向了它就不怎么收录,不管它了,我现在都用google。
[Reply]
我在使用Dean’s Permalinks Migration 时出现错误:
http://www.wordpress.org.cn/thread-12239-1-1.html
请你帮忙解决一下。谢谢!
[Reply]
这插件不就是用的301…
[Reply]
http://www.6inchboot.com/
UGG boots;cheap UGG;UGG classic
[Reply]