0

I have a simple installation of WooCommerce that I need to display under two different URLs like so

https://www.something.com/shop/category/product

https://www.something.com/knowledge/category/product

I have managed to set the base URL for the /shop using the following and with some permalink bases:

    add_filter('rewrite_rules_array', function($rules) {
        $new_rules = array(
            'shop/([^/]*?)/page/([0-9]{1,})/?$' => 'index.php?product_cat=$matches[1]&paged=$matches[2]',
            'shop/([^/]*?)/?$' => 'index.php?product_cat=$matches[1]'
        );
        return $new_rules + $rules;
    });

However I want to also be able to access the shop at /knowledge without any URL changes.

This works, however it performs a redirect rather than displaying the page at the entered URL.

EG: https://www.something.com/knowledge/product/someproduct/

redirects to:

https://www.something.com/shop/product/someproduct/

    function kvRewrite() {
        add_rewrite_rule('knowledge/([^/]*?)/(.*)', 'index.php?pid=$matches[2]', 'top');
    }
    add_action('init', 'kvRewrite');

    function kvRewriteQueryVars($query_vars)
    {
        $query_vars[] = 'pid';
        return $query_vars;
    }
    add_filter('query_vars', 'kvRewriteQueryVars' );

    function kvQueryRequest($query_vars)
    {
        if(array_key_exists('pid', $query_vars)) {
            $kv_vars = get_posts(array(
                'post_type' => 'product',
                'name' => $query_vars['pid']
            ));

            if($kv_vars) {
                $query_vars['p'] = $kv_vars[0]->ID;
                unset( $query_vars['pid'] );
            }
        }
        return $query_vars;
    }
    add_filter('request', 'kvQueryRequest');

Is there a way to do this?

Your Answer

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

Browse other questions tagged or ask your own question.