NeuroAgent

How to Remove /blog/ from URL in WordPress

Complete guide to removing /blog/ from WordPress URLs. Learn how to set up clean URLs without unnecessary prefixes using settings, code, and plugins.

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?

NeuroAgent

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.

The most common reason for “/blog/” appearing in URLs is WordPress’s default permalink structure. To solve this issue, start with the basic settings:

  1. Go to Settings → Permalinks in your WordPress admin panel
  2. Select “Post name” or “Custom structure”
  3. For “Post name” structure, set the option to /%postname%/
  4. 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:

php
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 URL
  • with_front => false - prevents adding post type prefixes
  • has_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:

php
// 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:

  1. Update existing URLs:

    • Use Permalink Manager plugin for bulk updates
    • Or create a custom script to update the database
  2. 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:

  1. Backup: Always create a database backup before making changes

  2. SQL query to update URLs:

    sql
    UPDATE wp_posts SET post_name = REPLACE(post_name, 'blog/', '') 
    WHERE post_name LIKE 'blog/%';
    
  3. Update taxonomy terms:

    sql
    UPDATE wp_terms SET slug = REPLACE(slug, 'blog/', '') 
    WHERE slug LIKE 'blog/%';
    

Testing Changes

After making all changes, you need to:

  1. Clear cache:

    • Clear plugin cache (if using WP Rocket, W3 Total Cache, etc.)
    • Clear browser cache
    • Check site functionality in incognito mode
  2. Check URLs:

    • Create a new post/page
    • Verify that URLs don’t contain “/blog/”
    • Check that old links work (should redirect to new ones)
  3. Test functionality:

    • Ensure all pages are accessible
    • Check navigation functionality
    • Test site search

Sources

  1. Permalink Manager Lite – WordPress plugin
  2. Custom Post Type Permalinks Plugin Documentation
  3. WordPress Codex - Post Types
  4. WordPress Codex - Rewrite API

Conclusion

To remove “/blog/” from WordPress URLs, follow these steps:

  1. Start with basic permalink settings in the admin panel
  2. When registering custom post types, use proper rewrite parameters
  3. If needed, use specialized plugins like Permalink Manager
  4. Set up redirects from old URLs to new ones
  5. 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.