I'm completely out of my league with regex stuff. I've seen a lot of examples for putting redirect code into the .htaccess file, but the default examples are mostly for changing away from dates. Everything else says to use the Redirection plugin, which I already use for simple 1 off redirects, but I don't know the regex for a permalink change. We have 743 posts, so I'm not doing those 1-by-1!

Here's the old and new structure:

old: /%post_id%/%postname%
new: /%category%/%postname%

Any help with what I need to put into the .htaccess file or the redirection plugin for this specific permalink change, would be most appreciated!

  • 1
    It's not possible to implement a redirect from /%post_id%/%postname% to /%category%/%postname% using .htaccess, simply because the %category% is unknown. You will need to perform this redirect in WordPress, since only WP knows what category the %postname% belongs to.
    – MrWhite
    Jun 14, 2018 at 14:52
  • That's what I was afraid of. When you say to perform it within WordPress, what are you thinking? I've seen some plugins that offer what I think will work but I was hoping to avoid adding more plugins.
    – user
    Jun 14, 2018 at 17:24

2 Answers 2


You can do this by regex rewriting the legacy URL as a query. You can do that either on the post_id or the postname, in this case I think the postname is probably safer (avoids false matches on some other path that might start with numbers).

In custom functions, something like this:

add_rewrite_rule( '^[0-9]+/(.*)/?', 'index.php?&name=$matches[1]','top' );

Some similar regex would work in .htaccess or a redirection plugin.

Basically this is rewriting the URL as a query and passing it on to WordPress. WordPress uses this to find the post and then returns the correct permalink as defined in your structure. Essentially it's two redirects.


I wouldn't do that, unless the website was a very small one, with few pages/posts, not planning any growth. It can be a bit better for SEO in terms of link structure, but there are speed penalties.

This article explains it nicely, though author's recommendation of using year/date is far from optimal, both in terms of speed and SEO I think.

/%post_id%/%postname% structure is as good as it gets for WordPress. Small sites, with a small number of pages are an exception - there go for the most SEO friendly link structure - whether just post name, or category/post-name, tag/postname etc - depending on your tag/category structure and content "arrangement" (is "logical sitemap" a valid English term?).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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