I am looking for the .htaccess regular expression that would redirect the old structure to the new one, the reason i don't want to use plugins is they often have unexpected results when WordPress updates or the author abandon them, I need something that would 301 redirect for a long time reliably.

Old permalink:


New Permalink:


Note: I am not sure if the rule should consider this but. I also use custom post types which add their slug to the url. So The "category" shows up for normal "post" but on CPT it's just:


I suspect a good rule could work for both cases since /%category%/ and /%example-cpt-slug%/ are something between "/ /".

What I'm not so sure is how can you specify the POST ID (%post_id%/) as a target.

  • 1
    You have to go with WordPress, nor the .htaccess, because the latter can't know the post ID. Take a look at some 301 plugins. – Max Yudin Feb 25 '17 at 14:04
  • It was my fear too, thanks for clarification. Feel free to put it in an answer i'll mark you as solution. – Michael Rogers Feb 25 '17 at 14:20
  • If you know an specific plugin that works for this particular case already it would also save time testing, this is a production site. Thanks again! – Michael Rogers Feb 25 '17 at 14:28

Within Settings > Permalinks your Custom Structure was set to /%category%/%postname%/ right?

Now you want to set (or you've already set) to /%post_id%/ right?

As far as I know (and I've also tested) WordPress will 301 redirect all of your posts (from the post type 'post') to your new Custom Structure. You won't need to do a thing.

So if you've set the Custom Structure to /%post_id%/ it means that if someone accesses the URL https://domain.com/my-category/my-post-slug they would be redirected to https://domain.com/POST_ID.

After you've changed your Custom Structure even if you also change your post slug WordPress will still redirect it correctly.

I don't know from which WP version this is supposed to work, but just for the record, I've tested within 4.7.

If it's not working for you let me know and I'll give you another solution.

By the way, I'm supposing your WordPress site URL didn't change as in your question you just used domain.com.

  • Yes same domain, the change is exactly as you described it, i just tried and something weird happens. I changed it and nothing changed on the site. Posts still have pretty permalinks (i'm logged in as admin i don't get served cache). I even changed it to "plain" (default) cos i know it can get stuck sometimes and back to /%post_id%/ but still seeing pretty permalinks. Weird right? – Michael Rogers Feb 25 '17 at 18:44
  • Could you copy and paste your .htaccess file content here? – filipecsweb Feb 25 '17 at 18:54
  • Oh wait this doesn't affect custom post types (95% of my posts) – Michael Rogers Feb 25 '17 at 18:54
  • No, it doesn't. – filipecsweb Feb 25 '17 at 18:57
  • 1
    If you send me the link of your new question I might be able to help you. – filipecsweb Feb 25 '17 at 19:05

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.