How To Change Permalink Structure From Postname To Category Postname

LAST UPDATED: January 3, 2017
Wordpress postname to category postname permalink fix

The most important SEO aspect of a blog is to have a clean and proper permalink structure. When you are going to have an ugly blog structure (First blog structure in the image below), it will definitely hurt your blog SEO rating. How could you choose the best link structure for your blog? It naturally depends on the nature and niche of your site.

Many bloggers will have a simple yet powerful postname blog structure (/%postname%/). The blog structure will be like this.

http://techiewheels.com/how-to-change-permalink-structure/

On the other hand, several bloggers will set the permalink that includes category and postname (/%category%/%postname%/) as blog structure which is the best blog structure in current trends. You might have come across sites have any of the two blog structure in common.

Mostly new sites will have a blog structure representing date and postname. You can change the permalink of your WordPress blog or site by heading to settings à permalink. There will be a lot of default options; however, you can set your own blog structure with custom blog structure option.

wordpress permalink blog structureHow To Change Permalink Structure From Postname To Category Postname

According to me the two above said blog structures are good and seems to be working for several blogs. In case, you pick to choose category and postname blog structure, you must be careful assigning a unique slug for each category in order to avoid duplication. In several instances, you might assign a post to two or more categories. So having the same slug name for different categories will result in duplication and might affect your site serp score.

It is recommended pick the best slog structure at the beginning of the site. It is not good changing the permalink structure when a site has loads of content. For instance, when your site has more than 2000 posts, changing the permalink structure would result in 404 error for the past indexed pages. So you need to point the old permalink structure to the new permalink structure using 301 redirect which is a permanent redirect.

There are several WordPress plugins to do this, but none of them are automatative meaning you have to manually add redirect to each and every post links. The best WordPress plugin is the Redirection plugin with Regrex.

You can also add a 301 redirect to htaccess file which doesn’t require adding redirect to all links manually. There is a tool developed by Yoast to generate 301 redirect, but it works only for %postname%.

When you want to change the permalink structure from /%postname%/ to /%category%/%postname%/ the tool doesn’t work at most times. It is annoying adding a 301 redirect for each link manually using the redirection plugin or similar plugins. But, you can make this happen by adding a few lines of code to the function.php file.

Here’s the code that you need to add to functions.php file.

function wpse_136306_redirect() {
    global $wp;

    // Check that we’ve hit a 404 for a non-empty permalink without slashes
    if ( is_404() && strlen( $wp->request ) && strpos( $wp->request, ‘/’ ) === false ) {
        // Attempt to find a post that matches the request.
        $posts = get_posts(
            array(
                ‘posts_per_page’ => 1,
                ‘name’ => $wp->request,
            )
        );

        if ( $posts && $url = get_permalink( $posts[0]->ID ) ) {
            // All good, let’s redirect to the new URL.
            wp_redirect( $url, 301 );
            exit;
        }
    }
}

add_action( ‘template_redirect’, ‘wpse_136306_redirect’, 1 );

Add the code to the last of the Functions.php and save a copy of the code which will save your blog when you update your theme. It works perfectly for one of my blog without slowing down the response time or server load time. It is the best way to change permalink structure from Postname to Category Postname at no time.

You can check whether the links are properly redirecting by accessing the old links from Google search. Filter all the indexed links from your site from Google by using this query “site:yoursitename”.

Do let us know whether the above method is working for you or not in the comments below.