WordPress/Woocommerce: How To Get The Original Url Before It’s Rewritten

Learning WooCommerce Development by Example
A Friendly and Accessible Guide to WooCommerce Development
Click here to get the book now for $29

WordPress has it’s own rewrite API which it uses to prettify URLs, to give a specific WooCommerce example the shop URL

http://wooexp.local/shop/

can be rewritten as

http://wooexp.local/index.php?post_type=product

If you type the URL above into a browser (changing the host from the I’m using on a Local by Flywheel test site to your site) then you should be taken to the shop page.

The idea of the rewrite API is that it takes the URLs that WordPress uses to construct queries and rewrites them so they are easier to read. This makes it easier for the user to  navigate and easily identify where they are in a site/store, it also aids SEO as search engines are able to easily see the intent of prettified URLS.

Recently I was trying to work back form a prettified URL to the original URL that WooCommerce/WordPress was working from but I couldn’t find an easy way to to get back to the original URL used.

It turned out the answer was pretty simple once I knew the correct properties to look for, which are the matched_rule and matched_query properties that are available on the global $wp object, here’s a snippet that will output the properties and a couple of other related properties at the bottom of the current page on your WooCommerce or WordPress site

add_action( 'shutdown','hwn_show_url_info');
function hwn_show_url_info(){
    global $wp;
    foreach ( array( 'request', 'matched_rule', 'matched_query', 'query_string' ) as $item ) {
        echo $item . ' - ' . $wp->$item . "<br>";
    }
}

Once I add the code above to my test store then I see the following output at the bottom of the page

The really excellent Query Monitor plugin also provides the debug information above (and a load more useful information as well)

Hopefully this article has provided you with information you need to see what’s going on behind the scenes with your site or store’s URLs, if you have any questions or further erecommendations on how to get more information then please don’t hesitate to leave them in the comments.

Hey there! Hope you enjoyed that article, thanks for sticking with it until the end 🙂

If you enjoy our WooCooomerce writing you might enjoy our book "Learning WooCommerce Development By Example", it's only $29 and contains a multitude of useful information for WooCommerce developers. Click here for more details.

You might also like to sign up for our newsletter, where you'll be kept to date with all our latest WooCommerce articles. Just complete the form below to get the latest Hard Working Nerd WooCommerce content delivered straight to your inbox.

Leave a Comment