---
description: >-
  https://stackoverflow.com/questions/43447175/woocommerce-how-to-remove-product-product-category-from-urls
---

# WooCommerce- How to remove product & product-category from urls? (ok)

<figure><img src="https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FUyO2xNfhnuBXvii3mHdw%2FIHD3X.png?alt=media&#x26;token=acbb84ca-cdca-431f-b58e-96e7b733135a" alt=""><figcaption></figcaption></figure>

```php
// Remove slug product-category
add_filter('request', function( $vars ) {
  global $wpdb;
  if( ! empty( $vars['pagename'] ) || ! empty( $vars['category_name'] ) || ! empty( $vars['name'] ) || ! empty( $vars['attachment'] ) ) {
    $slug = ! empty( $vars['pagename'] ) ? $vars['pagename'] : ( ! empty( $vars['name'] ) ? $vars['name'] : ( !empty( $vars['category_name'] ) ? $vars['category_name'] : $vars['attachment'] ) );
    $exists = $wpdb->get_var( $wpdb->prepare( "SELECT t.term_id FROM $wpdb->terms t LEFT JOIN $wpdb->term_taxonomy tt ON tt.term_id = t.term_id WHERE tt.taxonomy = 'product_cat' AND t.slug = %s" ,array( $slug )));
    if( $exists ){
      $old_vars = $vars;
      $vars = array('product_cat' => $slug );
      if ( !empty( $old_vars['paged'] ) || !empty( $old_vars['page'] ) ) $vars['paged'] = ! empty( $old_vars['paged'] ) ? $old_vars['paged'] : $old_vars['page'];
      if ( !empty( $old_vars['orderby'] ) ) $vars['orderby'] = $old_vars['orderby'];
      if ( !empty( $old_vars['order'] ) ) $vars['order'] = $old_vars['order'];    
    }
  }
  return $vars;
});
```

## [WooCommerce- How to remove product & product-category from urls?](https://stackoverflow.com/questions/43447175/woocommerce-how-to-remove-product-product-category-from-urls)

[Ask Question](https://stackoverflow.com/questions/ask)Asked 5 years, 6 months agoModified [1 year, 1 month ago](https://stackoverflow.com/questions/43447175/woocommerce-how-to-remove-product-product-category-from-urls?lastactivity)Viewed 78k times9

I'm using WooCommerce on a WordPress and it adds product & product-category to the

**URLs.**

[http://dev.unwaveringmedia.com/8dim/product-category/all-party-supplies/](http://dev.unwaveringmedia.com/8dim/product-category/all-party-supplies/) [http://dev.unwaveringmedia.com/8dim/product/14-snowman-serving-tray/](http://dev.unwaveringmedia.com/8dim/product/14-snowman-serving-tray/)

I need to remove 'product' & 'product-category' from the URLs. Is there any way to modify the permalinks and remove them?

* [wordpress](https://stackoverflow.com/questions/tagged/wordpress)
* [woocommerce](https://stackoverflow.com/questions/tagged/woocommerce)

[Share](https://stackoverflow.com/q/43447175/11373011)[Edit](https://stackoverflow.com/posts/43447175/edit)FollowFlag[edited Sep 7, 2020 at 6:55](https://stackoverflow.com/posts/43447175/revisions)[![General Grievance's user avatar](https://www.gravatar.com/avatar/d97f2d5c622f069ac28762c6a8250e93?s=64\&d=identicon\&r=PG)](https://stackoverflow.com/users/4294399/general-grievance)[General Grievance](https://stackoverflow.com/users/4294399/general-grievance)4,3312424 gold badges2828 silver badges4343 bronze badgesasked Apr 17, 2017 at 7:30[![Bhagya Shree 's user avatar](https://www.gravatar.com/avatar/830158e4eb2c6c132ccc86376b7ae837?s=64\&d=identicon\&r=PG\&f=1)](https://stackoverflow.com/users/7877426/bhagya-shree)[Bhagya Shree](https://stackoverflow.com/users/7877426/bhagya-shree)9111 gold badge11 silver badge33 bronze badges

* Have you checked settings ->permalinks? – [Yahya Hussein](https://stackoverflow.com/users/5077196/yahya-hussein) [Apr 17, 2017 at 7:35](https://stackoverflow.com/questions/43447175/woocommerce-how-to-remove-product-product-category-from-urls#comment73950278_43447175)
* @Bhagya Shree it's not recommended, read this [docs.woocommerce.com/document/…](https://docs.woocommerce.com/document/removing-product-product-category-or-shop-from-the-urls/) – [kebir](https://stackoverflow.com/users/3665987/kebir) [Apr 17, 2017 at 8:03](https://stackoverflow.com/questions/43447175/woocommerce-how-to-remove-product-product-category-from-urls#comment73950901_43447175)&#x20;
* you should use of woocommerce seo perfect plugin because it's good for seo and create best url for you I use most way to remove category but I saw there are dot in canonical so i decided to use this plugin cheers – [amh](https://stackoverflow.com/users/11807553/amh) [Sep 6, 2020 at 14:05](https://stackoverflow.com/questions/43447175/woocommerce-how-to-remove-product-product-category-from-urls#comment112765695_43447175)

[Add a comment](https://stackoverflow.com/questions/43447175/woocommerce-how-to-remove-product-product-category-from-urls)

### 6 Answers

Sorted by:                                              Highest score (default)                                                                   Trending (recent votes count more)                                                                   Date modified (newest first)                                                                   Date created (oldest first)                              23

Yes. But Please read this article first [https://docs.woocommerce.com/document/removing-product-product-category-or-shop-from-the-urls/](https://docs.woocommerce.com/document/removing-product-product-category-or-shop-from-the-urls/)

You can change this by:

1. you can change the permalinks in Settings > permalink > optional > Product category base= ./ (type ./ in Product category base).
2. Be sure that you don’t have any page, post or attachment with the same name (slug) as the category page or they will collide and the code won’t work.
3. Install and activate the plugin below: (For more info please see [https://timersys.com/remove-product-category-slug-woocommerce/](https://timersys.com/remove-product-category-slug-woocommerce/))

\`

```php
<?php
/*
Plugin Name: Remove product-category slug
Plugin URI: https://timersys.com/
Description: Check if url slug matches a woocommerce product category and use it instead
Version: 0.1
Author: Timersys
License: GPLv2 or later
*/
add_filter('request', function( $vars ) {
    global $wpdb;
    if( ! empty( $vars['pagename'] ) || ! empty( $vars['category_name'] ) || ! empty( $vars['name'] ) || ! empty( $vars['attachment'] ) ) {
        $slug = ! empty( $vars['pagename'] ) ? $vars['pagename'] : ( ! empty( $vars['name'] ) ? $vars['name'] : ( !empty( $vars['category_name'] ) ? $vars['category_name'] : $vars['attachment'] ) );
        $exists = $wpdb->get_var( $wpdb->prepare( "SELECT t.term_id FROM $wpdb->terms t LEFT JOIN $wpdb->term_taxonomy tt ON tt.term_id = t.term_id WHERE tt.taxonomy = 'product_cat' AND t.slug = %s" ,array( $slug )));
        if( $exists ){
            $old_vars = $vars;
            $vars = array('product_cat' => $slug );
            if ( !empty( $old_vars['paged'] ) || !empty( $old_vars['page'] ) )
                $vars['paged'] = ! empty( $old_vars['paged'] ) ? $old_vars['paged'] : $old_vars['page'];
            if ( !empty( $old_vars['orderby'] ) )
                    $vars['orderby'] = $old_vars['orderby'];
                if ( !empty( $old_vars['order'] ) )
                    $vars['order'] = $old_vars['order'];    
        }
    }
    return $vars;
});`
```

[![enter image description here](https://i.stack.imgur.com/IHD3X.png)](https://i.stack.imgur.com/IHD3X.png)

For more info please see [https://timersys.com/remove-product-category-slug-woocommerce/](https://timersys.com/remove-product-category-slug-woocommerce/)

[Share](https://stackoverflow.com/a/43963305/11373011)[Edit](https://stackoverflow.com/posts/43963305/edit)FollowFlaganswered May 14, 2017 at 11:21[![Shehroz Altaf's user avatar](https://lh4.googleusercontent.com/-cgsLwOn3mDU/AAAAAAAAAAI/AAAAAAAAAA0/7SBW1eUOxw4/photo.jpg?sz=64)](https://stackoverflow.com/users/6166802/shehroz-altaf)[Shehroz Altaf](https://stackoverflow.com/users/6166802/shehroz-altaf)60811 gold badge1010 silver badges1616 bronze badges

* 1Worked for me on latest Woocommerce 3.3.3. Thanks – [Dr.MTR](https://stackoverflow.com/users/7101936/dr-mtr) [Mar 3, 2018 at 21:29](https://stackoverflow.com/questions/43447175/woocommerce-how-to-remove-product-product-category-from-urls#comment85184339_43963305)
* how to install this plugin? – [ccdiego5](https://stackoverflow.com/users/4241525/ccdiego5) [Mar 25, 2019 at 12:57](https://stackoverflow.com/questions/43447175/woocommerce-how-to-remove-product-product-category-from-urls#comment97401810_43963305)
* The class members should be properly isolated from the string: `"SELECT t.term_id FROM " . $wpdb->terms . "...` – [Daniel W.](https://stackoverflow.com/users/1948292/daniel-w) [Nov 23, 2019 at 17:36](https://stackoverflow.com/questions/43447175/woocommerce-how-to-remove-product-product-category-from-urls#comment104268718_43963305)&#x20;

[Add a comment](https://stackoverflow.com/questions/43447175/woocommerce-how-to-remove-product-product-category-from-urls)Report this ad3

Noone mentioned how to remove /product/ so here it goes:

```php
function na_remove_slug( $post_link, $post, $leavename ) {
    if ( 'product' != $post->post_type || 'publish' != $post->post_status ) {
        return $post_link;
    }
    $post_link = str_replace( '/product/', '/', $post_link );
    return $post_link;
}
add_filter( 'post_type_link', 'na_remove_slug', 10, 3 );

function change_slug_struct( $query ) {

    if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
        return;
    }

    if ( ! empty( $query->query['name'] ) ) {
        $query->set( 'post_type', array( 'post', 'product', 'page' ) );
    } elseif ( ! empty( $query->query['pagename'] ) && false === strpos( $query->query['pagename'], '/' ) ) {
        $query->set( 'post_type', array( 'post', 'product', 'page' ) );

        // We also need to set the name query var since redirect_guess_404_permalink() relies on it.
        $query->set( 'name', $query->query['pagename'] );
    }
}
add_action( 'pre_get_posts', 'change_slug_struct', 99 );
```

I've added priority since some themes/plugins can conflict with this rule, which happened to me.

[Share](https://stackoverflow.com/a/69061288/11373011)[Edit](https://stackoverflow.com/posts/69061288/edit)FollowFlaganswered Sep 5, 2021 at 7:13[![Goran Jakovljevic's user avatar](https://www.gravatar.com/avatar/1dbf65144148446d5dfbbfe8c931aa26?s=64\&d=identicon\&r=PG)](https://stackoverflow.com/users/1301529/goran-jakovljevic)[Goran Jakovljevic](https://stackoverflow.com/users/1301529/goran-jakovljevic)2,49311 gold badge2626 silver badges2424 bronze badges

* This is a simple solution but it doesn't work if there is %product\_cat% in the product base and there are multiple categories in the URL, for example: /parent-cat/child-cat/product-slug/ – [Hasan Akhtar](https://stackoverflow.com/users/3871020/hasan-akhtar) [Oct 23, 2021 at 7:30](https://stackoverflow.com/questions/43447175/woocommerce-how-to-remove-product-product-category-from-urls#comment123175416_69061288)

[Add a comment](https://stackoverflow.com/questions/43447175/woocommerce-how-to-remove-product-product-category-from-urls)2

It wasn't working with pagination for me. Here is full code that's working.

```php
add_filter('request', function( $vars ) {
global $wpdb;

if( ! empty( $vars['pagename'] ) || ! empty( $vars['category_name'] ) || ! empty( $vars['name'] ) || ! empty( $vars['attachment'] ) ) {
    $slug = ! empty( $vars['pagename'] ) ? $vars['pagename'] : ( ! empty( $vars['name'] ) ? $vars['name'] : ( !empty( $vars['category_name'] ) ? $vars['category_name'] : $vars['attachment'] ) );
    $exists = $wpdb->get_var( $wpdb->prepare( "SELECT t.term_id FROM $wpdb->terms t LEFT JOIN $wpdb->term_taxonomy tt ON tt.term_id = t.term_id WHERE tt.taxonomy = 'product_cat' AND t.slug = %s" ,array( $slug )));
    if( $exists ){
        $old_vars = $vars;
        $vars = array('product_cat' => $slug );
        if ( !empty( $old_vars['paged'] ) || !empty( $old_vars['page'] ) )
            $vars['paged'] = ! empty( $old_vars['paged'] ) ? $old_vars['paged'] : $old_vars['page'];
        if ( !empty( $old_vars['orderby'] ) )
            $vars['orderby'] = $old_vars['orderby'];
        if ( !empty( $old_vars['order'] ) )
            $vars['order'] = $old_vars['order'];
    }
}
return $vars;
});
```

This will add new rewrite rule that will paginate query.

```php
add_action('init', 'do_rewrite');
function do_rewrite(){
add_rewrite_rule( '[^/]+/([^/]+)/page/?([0-9]{1,})/?$', 'index.php?attachment=$matches[1]&paged=$matches[2]', 'top' );
add_filter( 'query_vars', function( $vars ){
    $vars[] = 'attachment';
    $vars[] = 'paged';
    return $vars;
} );}
```

[Share](https://stackoverflow.com/a/59843980/11373011)[Edit](https://stackoverflow.com/posts/59843980/edit)FollowFlaganswered Jan 21, 2020 at 15:08[![Alex's user avatar](https://lh3.googleusercontent.com/a-/AAuE7mBt3hZvDQJq2lepm2QDULBXBgP88aTjtPrcfr8a0A=k-s64)](https://stackoverflow.com/users/12755399/alex)[Alex](https://stackoverflow.com/users/12755399/alex)2111 bronze badge[Add a comment](https://stackoverflow.com/questions/43447175/woocommerce-how-to-remove-product-product-category-from-urls)1

Using Shehroz Altaf's trick does the job just (almost) perfectly.

Adding the following between the `$slug` and `$exists` declarations will make it work with sub-categories too.

```php
$slug_array = explode( '/', $slug );
$slug = array_values(array_slice($slug_array, -1))[0];
```

[Share](https://stackoverflow.com/a/55239241/11373011)[Edit](https://stackoverflow.com/posts/55239241/edit)FollowFlag[edited Mar 19, 2019 at 11:11](https://stackoverflow.com/posts/55239241/revisions)[![AS Mackay's user avatar](https://i.stack.imgur.com/TVvYo.png?s=64\&g=1)](https://stackoverflow.com/users/7891828/as-mackay)[AS Mackay](https://stackoverflow.com/users/7891828/as-mackay)2,79399 gold badges1818 silver badges2525 bronze badgesanswered Mar 19, 2019 at 10:52[![Manu Garrido's user avatar](https://lh3.googleusercontent.com/-KrM1XJOg9H4/AAAAAAAAAAI/AAAAAAAAAA0/oEUG3FHpGFU/photo.jpg?sz=64)](https://stackoverflow.com/users/8225952/manu-garrido)[Manu Garrido](https://stackoverflow.com/users/8225952/manu-garrido)1144 bronze badges[Add a comment](https://stackoverflow.com/questions/43447175/woocommerce-how-to-remove-product-product-category-from-urls)1

You can use the following filter: woocommerce\_register\_post\_type\_product

Here is an example if you only want to remove /product/ but keep the category.

```php
add_filter( 'woocommerce_register_post_type_product', function($var) {
    $var['rewrite'] = str_replace('/product/', '/', $var['rewrite']);
    return $var;
});
```

Then make sure to flush the permalinks by updating the permalinks in the Admin panel.

Keep in mind you will still see '/product/' in the permalink structure in the Admin Panel Permalinks, but it will not have an affect.

[Share](https://stackoverflow.com/a/64176684/11373011)[Edit](https://stackoverflow.com/posts/64176684/edit)FollowFlaganswered Oct 2, 2020 at 18:38[![ggedde's user avatar](https://i.stack.imgur.com/ASptu.png?s=64\&g=1)](https://stackoverflow.com/users/2017192/ggedde)[ggedde](https://stackoverflow.com/users/2017192/ggedde)54455 silver badges1111 bronze badges

* How to remove the product name and only have the product id? – [ZeroCode](https://stackoverflow.com/users/5454048/zerocode) [Dec 28, 2020 at 6:44](https://stackoverflow.com/questions/43447175/woocommerce-how-to-remove-product-product-category-from-urls#comment115755036_64176684)

[Add a comment](https://stackoverflow.com/questions/43447175/woocommerce-how-to-remove-product-product-category-from-urls)0

Other solutions all failed for me, after some trial and error I came up with this solution...

1. Install free 'WOO CATEGORY BASE PERMALINK FIXER' plugin from [https://masterns-studio.com/code-factory/wordpress-plugin/woo-category-base/](https://masterns-studio.com/code-factory/wordpress-plugin/woo-category-base/)
2. Set Product Category Base Permalinks to : shop
3. Set Custom Base Permalinks to : /shop/%product\_cat%/

URLs will then look like

```php
http://www.example.com/shop/category/sub-category/product
```

Seems to work fine for pagination and sub categories
