301 Redirect for Blog Category

I’m using the antimatter theme and currently my blog posts have urls like this: mydomain.com/blog/my-post but I would prefer they have the url mydomain.com/blog/category-of-post/my-post. I understand that I will likely have to use a 301 redirect, but I’m not 100% sure how to implement this. Does anyone have any ideas about the code I have to use in .htaccess? I googled examples but none of them seemed to apply to my case. I’m also not sure if other changes would need to be made as well. For example, I use the tags plugin and currently the url of tags are mydomain.com/blog/tag:selected-tag. Would a 301 redirect for blog posts also change this?

If you want to have a category in the url, why not just structure your folder structure with categories folder under blog/ folder?

I suppose that would work too, don’t know why I didn’t think of that. So under the blog folder, create 01.cat1, 02.cat2, and so on. Then in each category subfolder in the blog folder, copy the blog.md and rename it to the category. And then make a copy of the blog.html.twig for each category? I did just that, but posts under categories don’t show up, the category page doesn’t show any posts, and any category taxonomies or tag taxonomies don’t work for posts in category folders. Is there anything else I need to edit to get it to work right?

Well, you are half right.

Just create folders under your main 01.blog/ folder for each category. E.g. 01.blog/dogs/, 01.blog/cats/ etc, put your blot posts into those, e.g. one might be: 01.blog/dogs/dogs-are-better-than-cats/.

NOTE: Remember that the 01., 02., etc. prefixes are to give order and by default make a page visible (so that it shows up in a menu). You don’t need to add prefix for blog posts.

Anyway, now in your 01.blog/blog.md file where you define your blog collection, instead of simply using content: items: '@self.children'(which only looks at the children of the current page), you would use '@self.descendants' which goes throw all the pages on down.

More information here…

Thank you for the information. This is actually what I did after I made that comment. It seems to work well. However, when making a blog post under a category folder and giving it a category taxonomy and a tag taxonomy, those taxonomies seem to not work well on the page. For example, the blog post /blog/dog/dogs-like-apples with a category dog and a tag of apple. The tag apple will show up in the sidebar under Popular Tags, but when clicking the tag, it leads to the URL /blog/dog/tag:apple. So if I were to make another post in /blog/cat/cats-love-apples with the tag apple, only one of those posts will show. This only happens if you’re currently browsing a category page or an article in a category. Browsing just /blog and clicking the tag apple will show all posts and use the URL /blog/tag:apple. However, browsing in the category dog, or any post inside of dog, will only show posts with the tag apple inside of dog and use the URL /blog/dog/tag:apple.

This is likely due to the line {% set active = uri.param(taxonomy) == tax ? ‘active’ : ‘’ %} inside of taxonomylist.html.t wig, in particular the “active” part. I tried making a few changes to the link below this line, to no avail. Would you happen to know how to set the active taxonomy to blog?

I was also looking at sidebar.html.twig at the line {% include ‘partials/taxonomylist.html.twig’ with {‘base_url’:new_base_url, ‘taxonomy’:‘tag’} %}. Could the tag be changing based on what page you’re on because of the “with {‘base_url’:new_base_url, ‘taxonomy’:‘tag’} %}” section?

This tag filtering needs to operate on the blog list view. That’s because they require a collection on the page to filter further. That’s how the filtering works on blogs normally. So for you sub-folder based pages, you would still need to point back to the main blog page to filter for the taxonomylist.

I’m not 100% sure what you mean by the popular tags issue. Perhaps you can PM me a zip of your user folder and I can use your examples to test it?

I have tags show up on the sidebar on the blog and all posts. I have two posts with two different categories, one in /blog/dog and one in /blog/cat. Both have the tag “Apple”. When I’m on /blog and press the Apple tag on the sidebar, I’m directed to /blog/tag:Apple and see both posts. When I’m browsing /blog/dog and press the the Apple tag in the sidebar, I’m directed to /blog/dog/tags:Apple and I only see one post, the post in the category dog. I would prefer that the tags remain consistent across all posts regardless of what category I’m in and have all tags be related to just blog, so I can see all posts with a particular tag, not just ones in a certain category.

I PM’d you a link to the ZIP. Thank you so much for all your help.