0

I'm facing the folloiwng problem:
I would like my products and product categories to have the same url base. E.g. www.example.com/products/product-category
www.example.com/products/sample-product

Trying to do this through the Permalinks section causes a 404 for all products. I know I can use a different name as a category base, e.g. 'foo' (www.example.com/foo/product-category), and then use the following code to re-write it to 'products':

add_filter('product_cat_rewrite_rules','update_base_category_rewrite_rule'); 
function update_base_category_rewrite_rule($category_rewrite){
    $new_category_rewrite = $category_rewrite;
    foreach($category_rewrite as $key => $value){
            $xyz = str_replace('foo', 'products', $key);
            $new_category_rewrite[$xyz] = $value; 
    } 
    return $new_category_rewrite; 

}

The problem with the above is that -as expected- all category links on the website are still in the form of www.example.com/foo/product-category

Any help with this would be highly appreciated.

0

You can not realistically do such a thing. Wordpress URL parsing is just not "smart" enough to be able to handle different types of content under the same "prefix". Wordpress will either try to parse the URLs as product or categories, depending on the specific order they are found in the rewrite table, and will fail on the other type.

If you have to have such a scheme you can override the rewrite handling and implement one of your own (there are probably few examples on this site), but I think the work required and the complexity of code introduced by such a thing will not be worth it (the code should not be very complex by itself, but whoever will maintain the code after you will suffer a big WTF moment trying to understand what is going on).

  • thank you very much for your reply! The thing is that I'm migrating a site from Drupal and need to keep the same structure for SEO purposes. The strange thing is that by using this code: gist.github.com/levantoan/fc705c5ae4739e6d87e2ec51b257ea5c I am able to get this structure working: www.example.com/products/product-category www.example.com/products/product-category/sample-product Which means that it is kind of possible to get the same base for both. I just can't figure out how to remove the /product-category/ before the sample product. Any ideas? – Bob Feb 28 '18 at 15:39
  • if the issue is SEO, just use 302 redirects for the relevant pages. – Mark Kaplun Feb 28 '18 at 15:46
  • the client insists on the same structure. It's not my decision, unfortunately. – Bob Mar 1 '18 at 5:11
  • In that case you will need to implement your own url parsing and appropriately set the main loop, and hopefully your client will need to pay more – Mark Kaplun Mar 1 '18 at 7:54
  • Thank you very much for all your help. Any guidelines as to how to do this? Maybe a link as to how this would work? It sounds a bit complicated to me to be honest...In any case, thanks!! – Bob Mar 1 '18 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.