Magento Router Url - Need Hyphnated Path Name

Magento Router Url - Need Hyphnated Path Name

August 30, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Router Url - Need Hyphnated Path Name

Customizing Magento Controller URLs: A Comprehensive Guide

As a senior developer working on a Magento project, you’ve likely faced the challenge of customizing URL paths to better match your business’s needs. In this post, we’ll dive deep into the process of renaming controllers and modifying configuration XML files to achieve hyphenated URLs in Magento.

Understanding the Default Behavior

By default, Magento follows a specific pattern for generating URLs based on your controller structure. If you have a controller file named TestController.php with an indexAction, the URL path will be /customcategory/test/index.

This is because Magento uses the camelCase naming convention of your controller and action to create the URL. The first part (after the category) comes from the controller name, and the second part is derived from the action.

The Challenge: Achieving Hyphenated URLs

Your goal is to rename the TestController or modify the configuration XML file so that you can have a hyphenated URL like /customcategory/test-section/index. This would allow you to include keywords in your URL, improving SEO and user experience.

To accomplish this, we need to understand how Magento handles routing at a deeper level. The key lies in how you define your controller and its corresponding route in the XML configuration file.

Modifying the Configuration XML

Open your module’s etc/frontend/routes.xml file (or create it if it doesn’t exist). This is where you can define custom routes for your controllers.

Here’s an example of what a standard route definition looks like:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <route id="customcategory_test_index">
        <frontName>customcategory</frontName>
    </route>
</config>

In this example, the id attribute corresponds to the URL path after the category (/test/index in our case), and the frontName attribute defines the category part of the URL.

Renaming Controllers for Hyphenated URLs

To achieve a hyphenated URL like /customcategory/test-section/index, you need to rename your controller according to Magento’s naming conventions. Here are the steps:

  1. Rename your controller file from TestController.php to TestSectionController.php. This is because Magento converts camelCase to hyphenated case for URLs.

  2. Update your route definition in the XML configuration file accordingly:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <route id="customcategory_testsection_index">
        <frontName>customcategory</frontName>
    </route>
</config>

Notice that we’ve changed the id attribute to match the renamed controller (TestSection).

  1. After updating your XML file, clear the Magento cache and check if your URL has been updated.

Best Practices for Custom Routing

When working with custom routes in Magento, it’s essential to follow some best practices:

  • Always define your routes in a separate module-specific XML configuration file.
  • Use descriptive names for your controllers and actions that reflect the content of the page.
  • Avoid using special characters or numbers in your controller or action names unless necessary. Stick to alphanumeric characters and hyphens.

Conclusion

In this post, we explored how to achieve hyphenated URLs in Magento by renaming controllers and modifying configuration XML files. By understanding the routing process and applying the correct naming conventions, you can create more SEO-friendly, user-centric URL structures that match your business needs.

Remember to keep your code organized and follow best practices for maintainability. Happy coding!

For more tips on Magento development, check out our comprehensive guides at Laravel Company.

Catalog Extensions Configuration SEO

Our network