Search code examples
wordpresscustomizationpermalinks

How to replace /category and /tag with /blog while adding /blog to posts in a WordPress URL without plugins


I would like to adjust the permalinks of my wordpress site without using a plugin. I want to get rid of "/category" and "/tag" while adding a prefix "/blog" to all blog related sites (category and tag overview plus posts).

I use several categories including sub-categories as well as tags but no custom post types. For example:

  • Category "animals" with sub-categories "dogs", "cats" and a tag "pet-food"
  • One post per sub-categorie, each tagged with "pet-food"
  • Hosted on www.example.com

For pages I don't need any customization resulting in www.example.com (/page-A). For posts the following structure would be ideal:

(1) www.example.com/blog; Showing all three posts

(2) www.example.com/blog/animals; Showing all three posts

(3) www.example.com/blog/animals/dogs; Showing the one post about dogs

(4) www.example.com/blog/animals/dogs/post-about-dogs; Reading the post about dogs

(5) www.example.com/blog/pet-food/; Showing all three posts

I tried so far several approaches. First of all I went to settings -> permalinks and chose the custom permalink option adding

/blog/%category%/%postname%/

while leaving the category and tag base options untouched. Here everything is working besides that "/category" and "/tag" still show up in cases 2,3 and 5.

By adding a "." into the optional category base the "/category" disappears in case 2 but now the url of case 3 (with and without "/category") isn't working. Case 5 is still unfixed.

By adding "/blog" instead of "." case 2 and 3 will work both without "/category" as intended but now cases 4 and 5 are broken.

I tried several other combinations with also changing the tag base and in none approach every link variation worked. I even tried a couple of times to reset the htaccess file in between different settings with no success.

I already considered Remove category & tag base from WordPress url - without a plugin and dozens of other sites. But unfortunately no configuration worked for me so far. I even tried to strip of the "/category" with the option provided by Yoast SEO (which is already installed and what can be used if needed) but this resulted into also cutting of "/blog".

Any help is much appreciated - it would be great if you have a solution for this problem!


Solution

  • I finally found a solution to this by myself.

    1. Go to Dashboard -> Settings -> Permalinks and use the custom permalink structure "/%category%/%postname%/"
    2. Change the tag base to "blog" leaving the category base untouched
    3. As I said I already had Yoast SEO so I used it to strip the word "category" for category overviews away (Yoast SEO -> Advanced [maybe you have to enable advanced options first] -> permalinks -> choose remove /category
    4. As I wanted "/blog" as base for all blog related posts I added the category "blog" as parent to all existing top categories
    5. Enjoy!

    Issues why this procedure is needed:

    1. /blog/%category%/%postname%/ as permalink structure is not working in combination with removing /category - at least if you are using a plugin for that (I tried several). I know that I wanted an approach without plugin but I found no code solution and I used Yoast anyways.
    2. You cas set your posts to "/blog/%category%/%postname%/". If you now change the category base to "." you don't have to eliminate the /category anymore. But this resulted into 404 errors while changing the base to "/blog" results into a wrong url structure.

    Issues with this approach:

    1. Your archives ordered by date will be reachable by example.com/year/ instead of example.com/blog/year