user5582011
user5582011

Reputation:

Redirect a post to it's root category in Wordpress

My blog posts can be in multiple categories, so they're accessable from

mywebsite.com/root-category/children-category-A/blog-post

and

mywebsite.com/root-category/children-category-B/blog-post

I need a redirect rule (in htaccess or with wp_redirect() function) which redirects the blog posts always to

mywebsite.com/root-category/blog-post

My attempt

$category_parent_check = get_the_category(); 
$category_parent_id = $category_parent_check[0]->category_parent;
if ( $category_parent_id != 0 ) {
    $category_parent = get_term( $category_parent_id, 'category' );
    $css_slug = $category_parent->slug;
} else {
    $css_slug = $category_parent_check[0]->slug;
}

if ($css_slug == "root-category") {
  wp_redirect(get_site_url() . "/root-category" . wp_make_link_relative( get_page_link())); exit;
}

I did this inside my single.php, but I didn't think about that $css_slug == "root-category" will always be true and an endless redirection is the result.

So how do I achieve that?

EDIT

my htaccess

<Files xmlrpc.php>
 Order Deny,Allow
 Deny from all
 </Files>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^root-category/[^/]+/(.+)$ $1/$2 [L,NC,R=301,NE]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Upvotes: 2

Views: 572

Answers (3)

anubhava
anubhava

Reputation: 785561

Since you want a full redirect with R flag, it is much easier to do that in a single redirect rule in your main .htaccess just below RewriteEngine On line:

RewriteEngine On

RewriteRule ^(root-category)/[^/]+/(.+)$ /$1/$2 [L,NC,R=301,NE]

# remaining rules go below this line

Make sure to test this in a new browser to avoid old cache.

Upvotes: 0

dineshkashera
dineshkashera

Reputation: 1502

From WordPress admin you can change your blog single Page url :

Go to

Settings >> Permalinks 

Select Custom Structure radio and enter this rewrite rule in text box

/%category%/%postname%/ 

I hope it will help you.

Upvotes: 0

raju_odi
raju_odi

Reputation: 1475

please use below code in your function.php so i think it will remove child category from permalink.

add_filter( 'post_link', 'my_filter_post_link' );

function my_get_all_child_categories( $category_id ) {

  $subcategories = array();

  $category = get_category( $category_id );
  if ( $category->parent ) {
    $subcategories[] = $category->slug . '/';
    $results = my_get_all_child_categories( $category->parent );
    $subcategories = array_merge( $subcategories, $results );
  }

  return $subcategories;
}

function my_filter_post_link( $permalink, $post, $leavename ) {

  if ( 'post' != get_post_type( $post ) )
    return $permalink;

  switch ( $post->post_type ) {
    case 'post':

      $categories = get_the_category( $post->ID );
      $subcategories = array();

      foreach ( $categories as $category ) {
        $results = my_get_all_child_categories( $category->term_id );
        $subcategories = array_merge( $subcategories, $results );
      }

      $permalink = str_replace( $subcategories, '', $permalink );

      break;
  }

  return $permalink;
}

Upvotes: 0

Related Questions