This tutorial will show you how to force rewrite WordPress URLs to use /blog/ in posts permalink structure. Websites that have a large number of pages, custom post types, posts, and posts categories may benefit by using the prefix “blog” in the URL instead of the blog category name slug. Most importantly, blogs that have posts in multiple categories, or categories that get removed or changed, should use this kind of URL structure.
For example, if a website’s blog has URLs that look like the following:
https://www.example.com/cooking-tips/top-5-cooking-tips
https://www.example.com/recipes/top-10-holiday-recipes
https://www.example.com/health-nutrition/cooking-for-healthy-living
The category slug name is replaced with “blog” and the URLs are rewritten to look like the following:
https://www.example.com/blog/top-5-cooking-tips
Posts Permalink Structure for SEO
It is recommended by Yoast SEO to use the category and the post name slug in the URL for the perfect WordPress SEO permalink structure. This can be accomplished by editing the permalinks in the WordPress admin area. There are a number of tags available to use. For the sake of this tutorial, use the following format:
/%category%/%postname%
To start, go to the Permalinks Settings page in the WordPress admin:
Settings > Permalinks
Next, add the tags into the custom structure field:
The above settings will output the category name and the post name in the permalink. The URLs will look like the following:
https://www.example.com/category-name/post-name
WordPress does not offer a simple mechanism or option to rewrite only the posts permalink structure. Editing the above Custom Structure field to force “/blog/%postname%” is an incomplete solution.
Replace Category Names with /blog/ in Posts Permalink Structure
The first part of the solution to adding the /blog/ prefix to the post URLs is to use the post_link WordPress filter to only modify the posts. The next step is to rewrite the URLs that include “blog/” by hooking into the generate_rewrite_rules WordPress function. The category name will be stripped from the posts permalink structure and be replaced with “blog”. Each posts attachment, embed, trackback, feed, page, and comments URL will also be rewritten to correctly resolve to the new post URL.
Your website’s paginated pages may not display correctly when creating a page with “blog” as the slug and using pagination. However, the first rewrite rule below solves this issue so that WordPress finds the next page before trying to find the single post.
Paste the following code snippet into the functions.php file of your active parent theme or child theme.
Final Thoughts
Use the Rewrite Rules Inspector plugin to check that WordPress is correctly using the updated rewrite rules.
Make sure to flush your rewrite rules after inserting the above code snippet in the functions.php file. Save the permalinks a couple of times in the WordPress admin area:
Settings > Permalinks
Need help? Our WordPress Development Service is a comprehensive solution for small, medium, and enterprise-level businesses. Call us at 602-633-4758 for a free consultation.
Hey there ! Thanks for your sharing ! I used it on the website http://www.cleram.com/blog but I’m facing an issue with the blog’s categories. I need to add an exception about the /blog/category-slug . Do you manage that in your script ?
Cheers
Hi John, the code snippet doesn’t manage blog categories. Instead, add “blog-category” to the Category base field in Settings > Permalinks.
Here is how the URL structure looks on our blog:
https://www.goldenoakwebdesign.com/blog-category/wordpress-tutorials-blog
My seo advisor did’nt want I change the prefix for something else.
But I managed something cool, here is my snippet to give you back your help 😉
-> I added that in your function golden_oak_web_design_blog_generate_rewrite_rules
$categories = get_categories( array(
‘orderby’ => ‘name’,
‘order’ => ‘ASC’
));
$exceptions_rules = array();
foreach ( $categories as $category ) {
$exceptions_rules[‘blog/’.$category->slug.’$’] = ‘index.php?category_name=’.$category->slug;
}
$merged_rules = array_merge($exceptions_rules, $new_rules);
…and of course an update…
$wp_rewrite->rules = $merged_rules + $wp_rewrite->rules;
Hi John, I’m glad you were able to modify the code snippet as a solution to your project. Very creative!
I’ve been looking for this solution all day. Setting category base didn’t work, but your snippet did. I appreciate you shared it here. Thanks, John!
Also, thank you, Golden Oak Web Design. Great tutorial. It worked like a charm for me too.
Hello Golden Oak Web Design and John, I appreciate your collaborations. But I am looking for a way to insert the category in the slug (blog/%category%/%postname%/), how would it be possible ?, the code snippet does not work.
Hi Andres, our example doesn’t do what you are hoping for. You may be able to edit John’s example to programmatically add the category: https://gist.github.com/loorlab/c46e57bb3298d0397588248ec8e04d0f
Very Nice Blog Post. Want to know more about permalink structure
Thank you so very much! I was looking for a way to do exactly this without having to create codes for custom post types.
I stuck to my standard permalink structure of “/%postname%/”, included the provided code in my theme’s function.php, and made sure to refresh my rewrites by saving the permalink structure without changing.
Everything works fabulously! Exactly what I was looking for!
I thinkk the admin of this web page is truly working hard in support of
his site, for the reason that here every stuff is quality based material.
Hi there, great article. I’ve read quite a lot of these in the past few days, but it is hard to pull off the best general-purpose SEO structure. Which is kind of weird considering WP is a large platform. I guess they are still mind-stuck in wp-is-only-a-blog wonderland.
Anyways, the general structure i am looking for is /blog/%category%/%postname%/. And yes, that includes three levels:
1. /blog/ is a Page which will be marked as the posts index.
2. /blog/%category%/ will be used as an archive page. Use only primary cat.
3. /blog/%category%/%postname%/ is the post itself.
4. /blog/tag/%tagname%/ and similar archives can be handled by the default prefix mechanism, since they are less important for SEO.
Sounds sane. But it proved impossible to pull off via built in permalink settings except if i set a category base to be “blog/./” which is really nasty. The user/googlebot does not notice of course, since nginx chews it up, but still. Nasty.
I did my best not to go the rewrites route, as adding more stuff you need to maintain in the future is just silly. But it does not seem that there are any other options at the moment. Has anyone tried modifying the gist provided by John (https://gist.github.com/loorlab/c46e57bb3298d0397588248ec8e04d0f) to support categories?
I will, but if anyone has any tips or solutions…. Im listening carefully 🙂
Why I can’t add WordPress blog posts as a subpage to /blog/ URL path? If I set the permalink, the URL path will go 404. But I if I change the /blog/URL path to anyother words, like /news/ URL path it will okay.
Hi JIms, the Rewrite Rules will work with a page named “Blog” and with the slug “blog”. My suggestion is to switch themes and disable plugins to determine if you have a conflict. Make sure to save the Permalinks (Settings > Permalinks) to flush the Rewrite Rules when testing. Also, make sure you don’t have a category or taxonomy named “blog”.
Why so hard?
just do it like https://gist.github.com/isuke01/f359d4bbf7e59d090ffcdf5bed5fa3f5
🙂
Thanks for suggesting an alternative to the rewrite method.
I added your function, but somehow it removes the edit button for the permalink in the backend. Any suggestions how to avoid this?
I found an improvement on the code. The part of “$post->post_name” on the return call needs to be exchanged with %postname%, otherwise the edit slug button won’t show. So it will be:
…
return home_url( ‘/blog/%postname%/’ );
…
Thanks for this. Worth noting that the code snippet will set the post URLs not to have a trailing slash. For the benefit of other readers, if you want a trailing slash, tweak line 41 to
return home_url( ‘/blog/’ . $post->post_name . ‘/’ );
Hi James, thank you for the suggestion.
HI
I would like to add the fixed word “/nl/ in each page, post, cpt
for example,
HOME_URL/nl/PAGE_SLUG
HOME_URL/nl/POST_SLUG
HOME_URL/nl/CATEGORY/CATEGORY_SLUG
….
How can I do that ?
Hi Jaydeep, if I’m understanding your question, it sounds like you would want to put the WordPress installation in a folder called “/nl/” and then set the “WordPress Address (URL)” and “Site Address (URL)” accordingly.