你可以很容易地重写
http://www.website.com/product/sony-tv-42-inch
到
http://www.website.com/product/sony-tv-42-inch
但额外的 / 会带来问题。
正如其他人所提到的,您当然希望 301 用于 SEO,所以这就是我要做的。
第 1 步 - 获取当前 URL:
获取所有当前帖子永久链接的列表。 David George 对 MySQL 的这个 SQL 查询应该可以解决问题。
SELECT wpp.post_title,
wpp.guid,
wpp.post_date,
CONCAT
(
wpo_su.option_value,
REPLACE
(
REPLACE
(
REPLACE
(
REPLACE
(
wpo.option_value,
'%year%',
date_format(wpp.post_date,'%Y')
),
'%monthnum%',
date_format(wpp.post_date, '%m')
),
'%day%',
date_format(wpp.post_date, '%d')
),
'%postname%',
wpp.post_name
)
) AS permalink
FROM wp_posts wpp
JOIN wp_options wpo
ON wpo.option_name = 'permalink_structure'
AND wpo.blog_id = 0
JOIN wp_options wpo_su
ON wpo_su.option_name = 'siteurl'
AND wpo_su.blog_id = wpo.blog_id
WHERE wpp.post_type = 'post'
AND wpp.post_status = 'publish'
ORDER BY wpp.post_date DESC
第 2 步 - 保存以备后用:
现在您已经将该列表转储到 Excel 列中以备后用。
第 3 步 - 将其格式化为 PHP:
获取所有 URL 并将它们转储到 Notepad++ 或同等设备中。在 Windows 上点击 cntrl+H 以调出查找/替换功能。确保选择“搜索模式”->“扩展”
对于“查找内容”,您应该输入 \r\n,对于“替换为”,您应该输入 , 并点击“全部替换”。
您现在应该有一个仅用逗号分隔的所有 URL 的列表。
第 4 步 - 创建 PHP 文件:
新建一个PHP文件并设置:
<?php
$urlString = "http://www.urlsample1.com/article/sony-thing-and-stuff, http://www.urlsample1.com/article/warner-brothers-stuff";
//this will replace the word article with the word product
$newstring = str_replace("article", "product", $urlString);
//turns string into array
$myArray = explode(',', $newstring);
//loops through array to find first case of - and turn it to a /
for ($i = 0; $i < count($myArray); ++$i) {
$haystack = $myArray[$i];
$needle = "-";
$replace = "/";
$pos = strpos($haystack, $needle);
if ($pos !== false) {
$finalstring = substr_replace($haystack, $replace, $pos, strlen($needle));
}
//removes common url elements
$newShort = str_replace("http://www.urlsample1.com", "", $finalstring);
$oldShort = str_replace("http://www.urlsample1.com/product", "/article", $myArray[$i]);
//prints out the old and new url in .htaccess format
print("Redirect 301 " . $oldShort . " " . $newShort . "</br>");
};
?>
第 5 步 - .Htaccess
上面的输出应该如下所示:
Redirect 301 /product/sony-thing-and-stuff /product/sony/thing-and-stuff
Redirect 301 /product/warner-brothers-stuff /product/warner/brothers-stuff
并且可以放入.htaccess文件中。
注意:这种方法在 Apache 上可能会变得很慢并且不是可取的,但它是我能想到的让您的客户端在新情况下启动并运行的最快方法。