How to set url for particular controller action - Magento

How to set url for particular controller action - Magento

September 22, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to set url for particular controller action - Magento

Setting Up Custom URLs for Magento Controller Actions

As a senior developer, I often encounter situations where clients need to set up custom URLs for specific controller actions within their Magento stores. The default URL structure is straightforward:

http://www.mywebsite/index.php/mymodule/index/subaction

However, there are times when you want to simplify the URL or change its structure without creating a new controller. In this post, I’ll guide you through setting up custom URLs for an existing action within your index controller.

Understanding the Default Behavior

By default, Magento uses the following URL pattern:

base_url/index.php/<module>/index/<action>

The <action> corresponds to the sub-action defined in your controller. For example, if you have mymodule with an IndexController, and it contains an action called subaction, the default URL would be:

http://www.mywebsite.com/mymodule/index/subaction

Setting Up Custom URLs

To set up custom URLs for your existing controller actions, you’ll need to use Magento’s routing system. Here are two common approaches.

  1. Using the Front Controller

You can override the front controller’s _loadRoute() method in your module configuration file (etc/config.xml). This allows you to map a specific URL to your desired action directly:

<config>
    <frontend>
        <routers>
            <mymodule>
                <use>standard</use>
                <args>
                    <module>Mymodule</module>
                    <frontName>mymodule</frontName>
                </args>
            </mymodule>
        </routers>
    </frontend>

    <global>
        <resources>
            <mymodule_setup>
                <setup>
                    <module>Mymodule</module>
                </setup>
            </mymodule_setup>
        </resources>
    </global>
</config>

In this example, the frontName is set to mymodule. As a result, you can access your sub-action using:

http://www.mywebsite.com/mymodule/subaction
  1. Using URL Rewrites

If you want to eliminate the module name from the URL entirely, you’ll need to create an URL rewrite entry.

First, in your mymodule/etc/config.xml, add a new node for the URL rewrites:

<config>
    <urlrewrite>
        <rewrite>
            <subaction>mymodule/index/subaction</subaction>
        </rewrite>
    </urlrewrite>
</config>

Then, run Magento’s URL rewrite generation script from your terminal:

php bin/magento indexer:reindex urlrewrite

After running this command, you should be able to access the subaction directly using:

http://www.mywebsite.com/subaction

Conclusion

Setting up custom URLs for existing controller actions in Magento is a straightforward process once you understand the platform’s routing system. By leveraging either the front controller override or URL rewrites, you can create clean and user-friendly URLs that improve your store’s navigation.

Remember to test these changes thoroughly in a staging environment before deploying them to your live site. If you’re looking for more advanced customizations and best practices for Magento development, don’t hesitate to check out our Magento Development Services at Laravel Company.

Happy coding!

Indexing Extensions Configuration CLI

Our network