0

Is it possible to create more SEO friendly filter URLS?

Instead of:

https://thejewelleryroom.com/product-category/rings/?filter_material=gold

Then:

https://thejewelleryroom.com/product-category/rings/rings-in-gold/

?

  • Yes, but it's probably easier to use Redirection.. – Sally CJ Feb 11 at 23:49
  • Cool. Thanks for sharing @SallyCJ. So you would basically make a redirection for the above two urls? And Google / Search engines would accept that? It's not too ideal, as the URL would still remain the same, even when shared, but it's at least better. I also thought about making some kind of subcategory, but not sure how that would work out. – FooBar Feb 12 at 6:22
  • Check my answer and let me know. :) – Sally CJ Feb 12 at 14:16
1

Yes, it is possible.

Option 1: Programmatically using add_rewrite_rule()

  1. Add the rewrite rule:

    /* The rule matches:
     * example.com/product-category/rings/rings-in-gold/
     * example.com/product-category/rings/rings-in-gold/page/<number>/
     */
    add_action( 'init', function () {
        add_rewrite_rule(
            '^product-category/rings/rings-in-gold(?:/page/(\d+)|)/?$',
            // $matches[1] is the (\d+) (page number) from the above Regex, if any.
            'index.php?product_cat=rings&filter_material=gold&paged=$matches[1]',
            'top'
        );
    } );
    

    For other categories, just copy the above add_rewrite_rule() and replace the text "rings", "rings-in-gold" and "gold".

  2. Register the custom query var filter_material:

    add_filter( 'query_vars', function ( $vars ) {
        $vars[] = 'filter_material';
        return $vars;
    } );
    

    You can use another name for the query var, but make sure the name does not already exist.

  3. Flush/regenerate the rewrite rules — just visit the permalink settings page.

And then in your theme/template, you can get the "material" filter value like so:

$material = get_query_var( 'filter_material' );

Option 2: Use the Redirection plugin

I'm not the author of the plugin and I have no affiliation with him or the plugin, but it's an awesome plugin with a user-friendly UI for creating redirections, both standard ones (where the URL in the browser's address bar changes) and pass-through which works like the add_rewrite_rule(), i.e. no URL change.

The plugin also comes with cool features such as tracking, so if you like that and don't want to mess with coding, then you can check here how to use the plugin. But here's an example based on the add_rewrite_rule() example above:

  1. Create a new redirection.

  2. Apply these settings: (click the cog icon to see the "When matched" option)

    Source URL: /product-category/rings/rings-in-gold(?:/page/(\d+)|)/?$
    Regex: Yes (check the box)
    When matched: Pass-through
    Target URL: /product-category/rings/?filter_material=gold&paged=$2
    

    Once again, for other categories, just replace the relevant text.

With this plugin, you don't need to register the custom query var (see step #2 in Option 1 above) and simply use $_GET['filter_material'] in your theme/template:

// Simplified example in PHP 7 syntax.
$material = $_GET['filter_material'] ?? '';

Note: Before you try the second option, make sure to edit your file and remove the code taken from Option 1 and visit the permalink settings page. Alternatively, use another category (e.g. earrings) when testing the second option. This is so that you know both options actually work. :)

|improve this answer|||||

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.