Skip to main content

How to add /blog/ as a prefix to permalink structure for blog posts in WordPress

Here is a snippet on how to turn all of your posts to new permalink structure without affecting other post types.
In this example I changed all my posts to have /blog/ in url.

This snippet should be added somewhere in your functions.php file.

Possible problems

You might bump into a problem with ‘Redirect loop’ after you add this code.
If you encounter that problem, just delete all from L31 > L45.

After adding these functions if you don’t do a redirect form ‘old url’ > ‘new url’, your posts will be accessible on both url’s, and from SEO point of view this is not good for you.
1. Duplicate content issue
2. There might be some links out there pointing back to your posts, and this is why you shoud 301 redirect them to new location.

30 comments

    1. Thanks. Haven’t seen this so far.
      Not sure why is this, but I believe you should be able to change slug on ‘Quick edit’ when viewing list of posts.
      That could do as a workaround?

    2. Blog previews are not working
      it only works after I published and unpublished it. Can you try with a new blog post? Hit preview.

      And when I removed that code (blog prefix) everything works fine.

      Please have a look into this issue ASAP

      Thank You

    1. Hi Sallie,
      Try just changing your permalinks under settings > permalinks. It might work with any of these too.

    1. I’m not sure is this some caching issue maybe?
      And depends which plugin are you using.
      Personally I haven’t tried so I’m not be able to give any advice at this moment, sorry.

  1. This is just fantastic. Just tested it on my develpment server and in works like charme.
    Thank you for sharing this live saving info.
    Deep Bow.

Leave a Reply

Your email address will not be published. Required fields are marked *

Please don’t paste any HTML, Js or PHP code into comments box, use pastebin.com or similar service to share code examples.

Characters left: 1000