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
Click here for more details about the "Learning WooCommerce Development By Example" book
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 recommendations on how to get more information then please don’t hesitate to leave them in the comments.
Click here to enroll in our Free WooCommerce Coding Course
Further Reading
https://gist.github.com/adamrosloniec/e34fcc7a0743769c75db1b072d677946 – this is a handy gist that returns a lot of useful information if you’re trying to debug the rewrite rules