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. Blogs that have posts in multiple categories, or categories that get removed or changed, are a smart reason to use this kind of URL structure.
For example, if a website’s blog has URLs that look like the following:
The category slug name is replaced with “blog” and the URLs are rewritten to look like the following:
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, but for the sake of this tutorial, use the following format:
Add the custom structure tags into the permalink settings page:
The above settings will output the category name and the post name in the URL for the permalinks and archives:
WordPress does not offer a simple mechanism or option to rewrite only the posts permalink structure. Editing the above Custom Structure 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.
Paste the following code example into the theme or child theme functions.php file:
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, by saving the permalinks a couple times in the WordPress admin area.
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.