I have a website with categories, subcategories, and of course products. The folder structure looks like this:
So the URL structures go something like:
I would like to change the url structures to look like this:
For category and subcategory:
Basically, replace ‘shop’ with ‘category’ for categories and subcategories. For the product, just remove ‘shop’ from URL.
How can I change the URL of subpages? Can you give me an example? I tried doing this myself using the documentation provided for routes but I’m a bit confused and I wasn’t able to get it to work the way I want.
I’m a bit confused, so you just want to get rid of shop, don’t you?
If so, you should be able to use the blog example but exchange
diruri to get the correct path to the page.
Yes, basically, but only for the product pages and for the categories/sub-category replace ‘shop’ with ‘category’.
(Replace ‘shop’ with ‘category’)
Category old url:
Category new url:
(Remove ‘shop’ from url completely)
Product old url:
Product new url:
To make this work, you need two routes:
- A route that listens on
category/(:all) and returns
return page('shop/' . $category)
- A route that simply “removes” the shop folder from the URL for the product pages. You can use the blog example from the routing docs for this.
Then you need to change the links to the products so that they use the correct URI scheme.
However please note that rerouting can lead to duplicate content if you don’t redirect the other URLs and it can also lead to unexpected results in other parts of your site since you potentially override top-level pages with the same name as a shop category.
Thanks, I got it working. One more question, since I’m a little confused on what routers are suppose to do exactly, aren’t they suppose to also change the links on pages too?
The routing is working how it’s suppose to but the links on the website are still the same as they were before.
For example, on my websites menu, the links to the category pages should now be
website.com/category/category-name but they still show up as
The router does not create URLs, the original URL still exists, it is only redirected. Therefore, you will have to adapt your links in your menus if you want them to show the “new” URL.
Edit: See this post for using a page model to automate this: