Magento - Custom Module Url

Magento - Custom Module Url

August 1, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Custom Module Url

Magento - Custom Module URL Change

One of the most common requests we receive is for changing the URL structure of Magento modules. In this post, I’ll guide you through the process of prepending a new path to your existing module URL.

Understanding Magento URL Structure

In Magento 2, each module has its own unique URL route defined in the config.xml file under the <frontend> node. By default, it follows this format:

<frontend>
    <routers>
        <anexample>
            <use>standard</use>
            <args>
                <module>An_Example</module>
                <frontName>anexample</frontName>
            </args>
        </anexample>
    </routers>
</frontend>

The <frontName> attribute defines the URL key for your module, in this case anexample. However, it doesn’t support prepending a path like hello/world.

The Solution: Custom Router Class

To achieve the desired prepended URL structure, you’ll need to create a custom router class. Here’s how:

  1. Create the Router File

In your module’s root directory, navigate to the etc folder and inside that, create a new folder named di. Inside di, create a PHP file named An_Example_Router.php.

  1. Implement the Custom Router

Open the An_Example_Router.php file and implement the following code:

<?php

namespace An\Example\Controller;

use Magento\Framework\App\ActionFactory;
use Magento\Framework\App\RequestInterface;

class Router implements \Magento\Framework\Router\RouterInterface
{
    /**
     * @var ActionFactory
     */
    protected $actionFactory;

    public function __construct(ActionFactory $actionFactory)
    {
        $this->actionFactory = $actionFactory;
    }

    public function match(RequestInterface $request)
    {
        $identifier = trim($request->getPathInfo());

        if (strpos($identifier, 'hello/world') !== false) {
            $request->setModuleName('anexample')
                ->setControllerName('Index')
                ->setActionName('index');
        }

        return null;
    }
}

Note that we’re checking if the requested URL contains hello/world. If it does, we set the module name to "anexample", the controller to Index, and the action to index.

  1. Update Module Configuration

Next, update your config.xml file:

<frontend>
    <routers>
        <anexample>
            <use>standard</use>
            <args>
                <module>An_Example</module>
                <frontName>hello/world</frontName>
            </args>
        </anexample>
    </routers>
</frontend>
  1. Update di.xml for Routing

Finally, update your di.xml file to include the new router:

<type name="Magento\Framework\Router\Standard">
    <arguments>
        <argument name="resource" xsi:type="string">An_Example::anexample</argument>
        <argument name="route" xsi:type="string">hello/world</argument>
    </arguments>
</type>

Conclusion

By following these steps, you should now be able to access your module using the prepended URL structure www.example.com/hello/world.

Remember that this is a basic example. For complex requirements, you might need to handle more advanced routing scenarios or use additional Magento 2 features.

As always, feel free to reach out if you have any further questions. You can find more detailed tutorials and guides on our Magento development blog.

Happy coding!

Magento 2 Extensions Configuration

Our network