Hello! I’m having an issue with setting up pretty URLs on my WordPress site. I want my links to look like https://sait.ru/usluga/, but WordPress automatically generates URLs like https://sait.ru/blog/service/usluga/.
I can tolerate having /service/ in the URL, but having /blog/ is completely unacceptable for my purposes.
I tried making changes to the functions.php file using conditional statements (if), but this causes regular pages to stop being created and only custom post types get created.
Could you please advise on how to solve this problem and remove /blog/ from URLs in WordPress?
WordPress automatically adds “/blog/” to URLs due to default permalink settings and post type structure. To remove “/blog/” from URLs, you need to change your permalink settings, use proper arguments when registering custom post types, or install special plugins to manage URL structure.
- WordPress Permalink Settings
- Registering Custom Post Types without /blog/
- Using functions.php to Modify URLs
- Plugins for URL Structure Management
- Handling Existing URLs
- Database and Redirects
- Testing Changes
WordPress Permalink Settings
The most common reason for “/blog/” appearing in URLs is WordPress’s default permalink structure. To solve this issue, start with the basic settings:
- Go to Settings → Permalinks in your WordPress admin panel
- Select “Post name” or “Custom structure”
- For “Post name” structure, set the option to
/%postname%/ - Save changes
Important: After changing permalink settings, WordPress will automatically create new URLs, but old links may continue to work. If you still see “/blog/” in URLs after these changes, the issue is likely related to post types or plugins.
Registering Custom Post Types without /blog/
When registering custom post types in WordPress, the rewrite parameter is key as it controls URL structure. To avoid adding “/blog/”, use the proper parameters:
function register_custom_service_post_type() {
$args = array(
'public' => true,
'has_archive' => true,
'rewrite' => array(
'slug' => 'service', // without /blog/
'with_front' => false // prevents adding /blog/
),
'menu_icon' => 'dashicons-admin-tools',
'supports' => array('title', 'editor', 'thumbnail')
);
register_post_type('service', $args);
}
add_action('init', 'register_custom_service_post_type');
Key parameters:
slug- defines the base part of the URLwith_front=>false- prevents adding post type prefixeshas_archive=>true- creates an archive page for the post type
Using functions.php to Modify URLs
If you’ve tried using conditional operators in functions.php but encountered problems, here’s a more appropriate approach:
// Remove /blog/ from URLs for all post types
function remove_blog_base($wp_rewrite) {
$rules = $wp_rewrite->rules;
// Find and remove rules containing /blog/
$new_rules = array();
foreach ($rules as $rule => $rewrite) {
if (strpos($rule, 'blog/') === false) {
$new_rules[$rule] = $rewrite;
}
}
$wp_rewrite->rules = $new_rules;
return $wp_rewrite;
}
add_filter('generate_rewrite_rules', 'remove_blog_base');
// Rewrite rules regeneration
function flush_rewrite_rules_after_theme_change() {
flush_rewrite_rules();
}
add_action('after_switch_theme', 'flush_rewrite_rules_after_theme_change');
Caution: This code may affect other plugins and site functionality. Always create a backup before making changes.
Plugins for URL Structure Management
There are special plugins that can help solve the “/blog/” in URL issue:
1. Permalink Manager Lite
This plugin allows you to manage URL structure for all post types and taxonomies:
- Automatically updates URLs when slug is changed
- Supports bulk URL updates
- Works with custom structures
- Allows creating clean URLs without unnecessary prefixes
2. Custom Post Type Permalinks
A specialized plugin for managing custom post type permalinks:
- Full control over URL structure
- Ability to exclude base prefixes
- Integration with SEO plugins
- Export/import settings
Handling Existing URLs
If you already have content with URLs containing “/blog/”, you need to:
-
Update existing URLs:
- Use Permalink Manager plugin for bulk updates
- Or create a custom script to update the database
-
Set up redirects:
php// Add redirects from old URLs to new ones add_action('template_redirect', 'old_blog_url_redirect'); function old_blog_url_redirect() { if (preg_match('/^\/blog\/(.*)$/', $_SERVER['REQUEST_URI'], $matches)) { wp_redirect(home_url('/' . $matches[1]), 301); exit(); } }
Database and Redirects
In some cases, direct database intervention may be required:
-
Backup: Always create a database backup before making changes
-
SQL query to update URLs:
sqlUPDATE wp_posts SET post_name = REPLACE(post_name, 'blog/', '') WHERE post_name LIKE 'blog/%'; -
Update taxonomy terms:
sqlUPDATE wp_terms SET slug = REPLACE(slug, 'blog/', '') WHERE slug LIKE 'blog/%';
Testing Changes
After making all changes, you need to:
-
Clear cache:
- Clear plugin cache (if using WP Rocket, W3 Total Cache, etc.)
- Clear browser cache
- Check site functionality in incognito mode
-
Check URLs:
- Create a new post/page
- Verify that URLs don’t contain “/blog/”
- Check that old links work (should redirect to new ones)
-
Test functionality:
- Ensure all pages are accessible
- Check navigation functionality
- Test site search
Sources
- Permalink Manager Lite – WordPress plugin
- Custom Post Type Permalinks Plugin Documentation
- WordPress Codex - Post Types
- WordPress Codex - Rewrite API
Conclusion
To remove “/blog/” from WordPress URLs, follow these steps:
- Start with basic permalink settings in the admin panel
- When registering custom post types, use proper
rewriteparameters - If needed, use specialized plugins like Permalink Manager
- Set up redirects from old URLs to new ones
- Thoroughly test all changes
The main problem when trying to use conditional operators in functions.php is the incorrect approach to URL rewriting. Instead, you should use WordPress hooks and specialized plugins designed specifically for URL structure management.