Create a custom route in Magento
Add a new route to your Magento store to handle specific URL patterns.
Fresh bytes every Thursday. No spam, ever. Unsubscribe anytime.
Join 9,000+ developers and get three free video lessons every week.
Custom routes tell Magento how to handle specific URL patterns. They're perfect for adding new pages or functionality to
your store. Let's create one that handles all URLs starting with /blog
.
We need to create a route configuration file named routes.xml
in our etc
directory, placing it in either the
frontend
area (for storefront pages) or adminhtml
area (for admin panel pages).
Since we're building a blog for customers to read, we'll create this in the frontend area:
Within this file we start with our root config
node — this tells Magento this is a configuration file. The schema
location in the XML header will validate our route config.
Next, we add a router
node with id="standard"
. This tells Magento to use its standard router, which handles most
frontend URL requests in your store.
Inside the router, we define our specific route using the route
node. This needs two key attributes:
frontName
: defines the URL path we want to match (in this case,blog
)id
: gives our route a unique identifier in the system (alsoblog
)
Pro tip: Using dashes in your URLs? That's fine for the
frontName
, but theid
attribute only allows letters, numbers, and underscores. So if yourfrontName
is "my-blog", yourid
should be "my_blog"."
Finally, we add the module
node to tell Magento which module should handle requests matching this route. We use our
module's full name in the standard Vendor_Module
format.
That's all you need! Now when someone visits /blog
, Magento knows exactly where to send that request. You can now
create controllers to handle different actions under this route.