【问题标题】:htaccess redirect - hundreds of postshtaccess 重定向 - 数百个帖子
【发布时间】:2016-11-11 05:59:03
【问题描述】:

Wordpress 重定向

我的客户有一个 WP 网站,其永久链接结构非常糟糕,并且他们创建了数百个“产品”页面,这些页面是带有一堆 HTML 的简单帖子。 例如:

http://www.website.com/article/sony-tv-42-inch
http://www.website.com/article/iphone-5-2-black
http://www.website.com/article/samsung-dvd-player-12455

我正在从头开始创建一个新网站,并计划为产品部分使用自定义帖子类型并组织 URL,例如:

http://www.website.com/product/sony/tv-42-inch
http://www.website.com/product/apple/iphone-5-black
http://www.website.com/product/samsung/dvd-player-12455

由于他不想失去任何流量或 SEO 评级,我想知道对于数百个帖子的 htaccess 重定向最简单的解决方案是什么? 如果他只有几十个,我可以手动完成,但几百个...... 另外,请记住,这是一个干净的 WP 安装,主题是从头开始构建的(我在本地工作,很可能会通过 CSV 文件导入产品),所以我不能只更改生产网站上的永久链接结构。

任何帮助将不胜感激

【问题讨论】:

    标签: wordpress .htaccess redirect


    【解决方案1】:

    你可以很容易地重写

    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 上可能会变得很慢并且不是可取的,但它是我能想到的让您的客户端在新情况下启动并运行的最快方法。

    【讨论】:

    • 您也可以使用正则表达式从单个重定向中处理它。选择文章并将其更改为“产品”将是 \/[article]{1,}\/ 但我不确定正则表达式是否仅处理“-”的第一个实例而不选择所有这些实例。这就是我选择这种方法的原因。如果您更熟悉正则表达式,则可以执行单个重写规则。
    【解决方案2】:

    对于 SEO,我建议您在 .htaccess 中使用 301 - 重定向

    您可以使用正则表达式进行重定向。

    例子:

    RewriteEngine on
    RewriteRule ^/users/(.*)$ http://www.example.com/profiles/$1 [R=301,L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-04
      • 2017-07-01
      • 2013-01-24
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      • 2011-09-25
      相关资源
      最近更新 更多