• Trying to setup a redirect from URL structure:

    brand/abc/[category]/
    to
    product-category/[category]/?filtering=1&filter_product_brand=69

    by using this function

    add_filter( 'rewrite_rules_array', function( $rules )
    {
        $new_rules = array(
            'brand/abc/([^/]+?)/?$' => 'product-category/$matches[1]?filtering=1&filter_product_brand=69',
            'brand/def/([^/]+?)/?$' => 'product-category/$matches[1]?filtering=1&filter_product_brand=68',
            'brand/ghi/([^/]+?)/?$' => 'product-category/$matches[1]?filtering=1&filter_product_brand=71',
        );
        return $new_rules + $rules;
    } ,20,1);

    This does a redirect to the target URL, but I would like to show the original URL () and show content of the target URL. So no 301 redirect. How can I do this? Is it better to do it in a function or in de .htaccess file?

    • This topic was modified 3 years, 1 month ago by ikwilhet.
    • This topic was modified 3 years, 1 month ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Everything else WordPress topic
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Rewriting where all parameters are in the query string and requesting index.php should result in no visible rewrite. The original URL remains with 200 status, but the query vars are all applied.
    'brand/abc/([^/]+?)/?$' => 'index.php?product-category=$matches[1]&filtering=1&filter_product_brand=69',

    I’m not sure if “product-category” is the right query var. If not, use the proper one and all should work.

    Thread Starter ikwilhet

    (@ikwilhet)

    @bcworkz I think I understand, but how do I figure out what the technical url is of the page that is shown as: /product-category/zonnepanelen/?filtering=1&filter_product_brand=69

    Because that is the url that is shown if I go to the category and then filter on brand.

    • This reply was modified 3 years, 1 month ago by ikwilhet.
    Moderator bcworkz

    (@bcworkz)

    Isn’t the goal for it to be brand/abc/zonnepanelen/ ? With the correct rewrite rule, WP will process it correctly, but links to the page that appear on the site will not use that structure on their own. You need to use the “term_link” filter or similar to alter the default structure to the one compatible with the rewrite rule.

    Aren’t there other brands that need to be rewritten besides “abc”? Is there an URL parameter that’ll filter on “abc” instead of 69? We could rewrite whatever comes after /brand/ to such an URL parameter. If one does not exist, it might be worth creating one. Then we could do something like
    'brand/([^/]+?)/([^/]+?)/?$' => 'index.php?product-category=$matches[2]&filtering=1&cust_filter_brand=$matches[1]',

    Thread Starter ikwilhet

    (@ikwilhet)

    That function does not work.

    Ie.
    brand/abc/solarpanels/
    needs to use the content of
    product-category/solarpanels/?filtering=1&filter_product_brand=69

    I will add more links to the array
    brand/def/invertors
    etc. etc.

    But the function doesnt work, redirects to the homepage. Where can I find the underlying url of product-category/solarpanels/?filtering=1&filter_product_brand=69
    You are sayin that the content of that page can also be found with index.php with some querystrings?
    is product-category a valid querystring then, is there a reference for this.

    I think I understand what you want to do, but I don’t know how to find the proper URL structure to use the content from.

    Moderator bcworkz

    (@bcworkz)

    It’s likely “product-category” is not the right query string var. I used it as an example, but I’m not familiar enough with your ecommerce system. The ecommerce plugin ought to have it documented. As a guess, try “product-cat” instead. If you use WooCommerce I think that’s the right query var.

    I’m not sure what you mean by underlying URL, but here’s an idea: switch your permalink settings to “plain” (make note of what it is now so it can be easily restored). The find a link to one the taxonomy’s terms. This will force the term to be passed as a query string var. Maybe this is what you mean by underlying URL? The links do not specify an actual file because the default index.php is the intended recipient of the request.

    When you rewrite to the /product-category/ route, where ever the request ends up is not the normal rewrite destination, so you naturally get a 302 status response. The normal rewrite destination is index.php, so there’s no 302 status involved.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Rewrite rules with WordPress function without 301 redirect’ is closed to new replies.