permalinks - Changing URL from %postname%%post_id to %category%%postname%%post_id%

I want to add categories to my URL structure. I'm sure this has been covered and I found many resources online to c

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


to


I added the following to my .htaccess:

RedirectMatch 301 ^/([^/]+)/(\d+)/$ /$1

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

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/this-is-a-post/1111

to

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

I added the following to my .htaccess:

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

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

Share Improve this question edited Oct 3, 2019 at 9:01 MrWhite 3,8911 gold badge20 silver badges23 bronze badges asked Oct 3, 2019 at 2:03 cjmicjmi 212 bronze badges 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 Commented Oct 3, 2019 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 Commented Oct 3, 2019 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 Commented Oct 4, 2019 at 11:00
  • Is there no way of adding /category/ without doing manual redirects? – cjmi Commented Oct 4, 2019 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 Commented Oct 5, 2019 at 17:25
 |  Show 1 more comment

1 Answer 1

Reset to default 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/<post slug>/<post ID> and example/<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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745103757a4611434.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信