2

I want to add categories to my URL structure. I'm sure this has been covered and I found many resources online to create the redirect code in .htaccess, but when I enter the code it doesn't work. Running an Apache server.

Essentially, I want to go from

https://example.com/this-is-a-post/1111

to

https://example.com/posts-category/this-is-a-post/1111

I added the following to my .htaccess:

RedirectMatch 301 ^/([^/]+)/(\d+)/$ https://example.com/$1

But I still get 404s when accessing links from outside sources (Google, Reddit etc)

6
  • 1
    Why not just use a custom structure - on the Permalink Settings page, just enter /%category%/%postname%/%post_id%/ in the Custom Structure box. – Sally CJ Oct 3 '19 at 5:00
  • 1
    "I found many resources online to create the redirect code in .htaccess" - You can't do that sort of redirect in .htaccess - where are you expected to get the posts-category part from? The above directive (apart from using the wrong directive with WordPress) would redirect to /this-is-a-post - which does not seem to be what you require. But the redirect is not necessary to get this to work anyway, only to preserve SEO. – MrWhite Oct 3 '19 at 8:55
  • We have over 2,000 articles and 55,000 backlinks to these articles. If I just change the permalinks I will have over 50k 404s which will break the site. – cjmi Oct 4 '19 at 11:00
  • Is there no way of adding /category/ without doing manual redirects? – cjmi Oct 4 '19 at 11:01
  • @cjmi, there is - use custom PHP/WordPress code like in my answer, in addition to changing the permalinks. I hope the answer helps and let me know how it goes. – Sally CJ Oct 5 '19 at 17:25
2

So first, I'm quoting @MrWhite's comment:

You can't do that sort of redirect in .htaccess - where are you expected to get the posts-category part from?

And he's right — you shouldn't use the .htaccess.

We have over 2,000 articles and 55,000 backlinks to these articles. If I just change the permalinks I will have over 50k 404s which will break the site.

You should just change the permalink structure and use custom code to redirect old URLs to the new ones.

Step 1: Change the permalink structure.

Just go to the Permalink Settings page and in the Custom Structure box, enter this:

/%category%/%postname%/%post_id%/

Step 2: Add custom PHP/WordPress code to redirect old URLs to new ones.

This is an example (tried and tested working) using the parse_request hook: (this would go in your theme functions.php file) (and the code uses these two WordPress functions: get_post_field() and wp_redirect())

add_action( 'parse_request', 'maybe_redirect_old_permalinks' );
function maybe_redirect_old_permalinks( $wp ) {
    if ( preg_match( '#^([^/]+)/(\d+)$#', $wp->request, $matches ) ) {
        list ( $path, $slug, $id ) = $matches;
        // Redirect to the new permalink, if the slug and ID match.
        if ( $slug === get_post_field( 'post_name', $id ) ) {
            wp_redirect( get_permalink( $id ), 301 );
            exit;
        }
    }
}

This approach also means that both example.com/<post slug>/<post ID> and example.com/<post category>/<post slug>/<post ID> would work. But on the page (post content, widgets, etc.), post permalinks would only be in the later format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.