Get in touch
Back to blog
September 9th, 2022

How to Rewrite the Taxonomy URL in WordPress

by Luca Reale
cropped-luca.png

Good WordPress taxonomy is a critical part of your site’s health. It improves two of the most important metrics for site owners: user experience and SEO. To improve your site’s taxonomy, you’ll want to use custom permalinks. But before you can do that, you’ll need to know how to rewrite the taxonomy URL in WordPress.

What is Taxonomy?

Website taxonomy is the system used to classify content in a website.

With taxonomy, content can be classified into categories or sections. For instance, you can organise a shoe’s website based on categories like women’s shoes (“example.com/women”) and men’s shoes (“example.com/men”).

Having a clear and organised taxonomy also makes it easier for people to use your site. 

The URL “www.example.com/categories/boots/hiking” shows the user how they got to where they are. From this URL, they know that if they go back one page, they’ll be on the page for boots. Or, if they want to go back to the categories page, they can simply go to “example.com/categories”.

On the other hand, we’ve all come across URLs that look a bit like this: “www.example.com/our-products/we-have-a-wide-selection-of-boots-available/07012021”.

As you can see, website taxonomy goes hand in hand with URL structure (the way URLs are organised to reflect the content within specific site pages).

Benefits of a Good Taxonomy URL

  • Having clean URLs and good taxonomy lets search engines crawl your website easily, potentially giving you a better site ranking.
  • Clean URLs are memorable, enabling users to find what they’re looking for easily.
  • Good taxonomy URLs help with a good information architecture that allows users to know where they are on the site and where the content they want is located.
  • Grouping your related web pages and content provides a solid foundation for your website and makes it easier to scale.

Tips for Creating Good Taxonomy URLs

  • Good URLs should be easy to follow for users and search engines. This means that you should shorten your URL by removing unnecessary information, such as dates.
  • Your URLs should include the keyword (you’re trying to rank that page for) to help improve your site rankings.
  • The URLs should make it easier to keep the site organised while scaling. They should be structured so that it’s easy to add new pages that fit within the context of the taxonomy.

Now that you know some tips for creating a good taxonomy URL let’s learn how you can rewrite the taxonomy URL on WordPress.

How Do WordPress URLs Work?

Before rewriting the taxonomy URL, you need to know how URLs work in WordPress.

If you change the WordPress permalink structure, WordPress automatically changes the htaccess file, sending all website traffic to the index.php file.

The index.php file then determines what resource you’re trying to access. It searches to see if the resource is in the file structure and takes you to it if it’s available.

If there’s no such resource, the index.php file checks the database and uses the rules you set (custom rules) and the current URL to know what to do: For example, if you set up your rewrite rules correctly, you’ll be redirected to the requested resource via a 301 redirect. If not, WordPress will return error 404, meaning the page or resource is unavailable.

How to Register a New Rewrite Rule

To rewrite the taxonomy URL on WordPress, you need to register a new rewrite rule using the built-in add_rewrite_rules() function and then pass it to the functions.php file for rendering.

To do this, first, open your functions.php file. 

You can find the functions.php file by accessing your WordPress Admin interface and clicking on Appearances, then Theme Editor. Theme Functions (functions.php) is located in the right sidebar under Theme Files.

Once you’ve opened the file

  • Create a rewrite rule using the following code.
add_action( 'init',  function() {
    add_rewrite_rule( 'myparamname/([a-z0-9-]+)[/]?$', 'index.php?myparamname=$matches[1]', 'top' );
} );
  • The next step is “flushing”. Flushing updates the WordPress database whenever you add new rewrite rules or change your site’s URL structure and enables WordPress to understand the new URL.

To flush your permalinks, head to your WordPress admin interface, click Settings, and then Permalinks. Finally, click Save Changes to refresh your website’s rewrite rules. 

  • Next up, you need to whitelist or allow the query parameter. So, add the following code to your functions.php file.
add_filter( 'query_vars', function( $query_vars ) {
    $query_vars[] = 'myparamname';
    return $query_vars;
} );
  • Finally, ensure you add a handler to send the query parameter to a template file.
add_action( 'template_include', function( $template ) {
    if ( get_query_var( 'myparamname' ) == false || get_query_var( 'myparamname' ) == '' ) {
        return $template;
    }

    return get_template_directory() . '/template-name.php';
} );

That’s it! You now have a new rewrite rule. The rewrite rule above specifies two conditions that, when fulfilled, return the path to the directory of the template file.

Enjoy your new URLs!

Good site taxonomy will benefit your site and make life easier for you and your users. Plus, you get the added bonus of gaining a slight SEO advantage! 

For more WordPress tips and tutorials, feel free to browse the rest of our blog.

Category:
cropped-luca.png
Luca Reale
Content writer

Our newsletter

Our links

Our Work

Our Content

Social

Copyright © 2024 Drewl (Luna Digital Ltd.). All rights reserved