【问题标题】:WP Redirect From /%postname%.html To /%category%/%postname%/WP 重定向从 /%postname%.html 到 /%category%/%postname%/
【发布时间】:2017-09-19 16:45:53
【问题描述】:

我将我的 WP 永久链接从“/%postname%.html”,而不是更常见的“/%postname%/”更改为目前的“/ %category%/%postname%/”。 WP 处理得很好。但是我在将旧 URL 重定向到新结构时遇到问题。我尝试了几个重定向插件,但它们似乎只在旧结构是最常见的“/%postname%/”时才会重定向。

当我尝试在浏览器中打开旧 URL 时出现 404 错误,除非我将“RewriteRule (.+).html$ /$1/ [L,R]” 添加到 .htaccess 以进行第一次更改从 /xxx.html/xxx/ 的 URL。我要解决的问题是,尽管一切正常,但我总共有 2 次重定向才能到达最终正确的“/%category%/%postname%/”。 Pagespeed Insights 将此显示为我应该修复的登录页面重定向问题。有没有办法在 .htaccess 中纠正这个问题?谢谢。

【问题讨论】:

    标签: wordpress .htaccess redirect plugins permalinks


    【解决方案1】:

    应该用这个插件来完成

    https://wordpress.org/plugins/seo-redirection/

    【讨论】:

    • 我看不到从旧永久链接结构到新结构的重定向。我不想一次添加一页重定向。
    【解决方案2】:

    我暂时不使用 Nginx 重定向,我使用这个

    add_filter( '404_template', 'custom_redirect_to_category' );
    
    function custom_redirect_to_category($template) {
    
        if ( ! is_404() ){
            return $template;
        }
    
        global $wp_rewrite;
        global $wp_query;
    
        if ( '/%category%/%postname%/' !== $wp_rewrite->permalink_structure ){
            return $template;
        }   
    
        if ( ! $post = get_page_by_path( $wp_query->query['category_name'], OBJECT, 'post' ) ){
            return $template;   
        }
    
        $permalink = get_permalink( $post->ID );
    
        wp_redirect( $permalink, 301 );
        exit;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      • 2017-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-12
      相关资源
      最近更新 更多