Redirecting of permalinks

Asked

Viewed 250 times

1

I have a site developed in Wordpress, and the main structure is: www.teste.com.br/titulo-do-post only, I’ll have to modify to: www.teste.com.br/categoria-do-post/titulo-do-post

All the links I shared as shown in the first form will no longer work, not to mention the relevance of Google. What should I do?

I’m changing it here: SS

  • 1

    as Voce is modifying the structure?

  • I changed the question.

  • You want to know how to implement the new structure or what you will go through with the predetermined one after implementing?

  • I want to know how to keep the old links. And make a redirect to the new link.

2 answers

2

The plugin Redirection serves to manage 301 redirects, which is what you need.

Another way to solve the problem, but that requires more knowledge, is to create Rewrite Rules directly in the configuration of your site in Apache, through the file .htaccess

  • From what I’ve seen, I’ll have to redirect link by link?

  • I particularly like this solution to use Rewrite Rules because the performance is much better than using any plugin or hook in Wordpress. In your case, doing a "de-stop" is not so difficult.

  • @Felipestoker if you decide to use Redirection, you will have to link to the link. The second solution is smarter, I strongly suggest reading the Apache mod_rewrite documentation and implementing it. Abs.

1

add Rules to categories:

add_action( 'init', 'nz_add_category_rules' );

function nz_add_category_rules() {

      $categories = get_categories( 'post' );
      $cat_slug = array();
      foreach ( $categories as $cat ) {
            $cat_slug[] = $cat->slug;
      }
      $cat_regex = '(' . join( '|', $cat_slug ) . ')';
      $regex = $cat_regex . '/([^/]+)$';
      $redirect = 'index.php?category_name=$matches[1]&name=$matches[2]';


      add_rewrite_rule( $regex, $redirect, 'top' );
}

add the filter to the post link:(the first category will be used)

add_filter( 'post_link', 'nz_get_permalink' );

function nz_get_permalink( $permalink ) {
      global $post;

      if ( $post->post_type == 'post' ) {
            $category = get_the_category();
            if ( isset( $category[ 0 ] ) ) {
                  $cat_slug = $category[ 0 ]->slug;
                  $permalink = trailingslashit( get_home_url() ) . trailingslashit( $cat_slug ) . $post->post_name;
            }
      }
      return $permalink;
}

redirect to new url:(it is only necessary if you want to redirect permanently since the preset permalink is still valid)

add_action( 'template_redirect', 'nz_redirect_posts', 100 );

function nz_redirect_posts() {
      global $wp_query;
      if ( $wp_query->get( 'name' ) && is_singular( 'post' ) && !$wp_query->get( 'category_name' ) ) {
            $permalink = get_permalink( $wp_query->post );
            wp_redirect( $permalink, 301 );
            exit;
      }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.