URL rewriting for Magento module

URL rewriting for Magento module

July 25, 2026 · Illustrated author avatarBy Stefan Bogdanescu
URL rewriting for Magento module

URL Rewriting for Magento Modules: A Comprehensive Guide

URL rewriting is a crucial aspect of improving your website’s SEO and user experience. In this blog post, we’ll guide you through the process of rewriting URLs for your custom Magento module without having to move actions between controllers.

Understanding the Current Issue

You’ve created a new Magento module called “article” with an “index” controller and an “article” controller that contains an “archives” action. The current URL structure looks like this:

http://mydemostore/article/article/archives/01/2011

However, you want to achieve a cleaner, more SEO-friendly URL:

http://mydemostore/article/archives/01/2011

Why the Current Structure Happened

The extra “article” in your URL is due to the fact that both your module and controller share the same name. If you had placed your actions inside the “index” controller, you would have achieved this structure.

However, moving actions between controllers might not be feasible for various reasons, such as maintaining a clean separation of concerns or avoiding conflicts with other modules.

Two Approaches to Solve Your Problem

Approach 1: Using Zend Routing

Magento uses the Zend Framework’s routing component. You can leverage this built-in functionality to rewrite your URLs without touching the .htaccess file.

Here are the steps:

  1. Open your module’s etc/config.xml file and add a new frontend <router> section, like this:
<config>
    <modules>
        <!-- ... other modules ... -->
    </modules>

    <frontName>
        <Magento_Theme>
            <identifier>theme</identifier>
        </Magento_Theme>

        <Magento_Backend>
            <identifier>admin</identifier>
        </Magento_Backend>

        <article>
            <identifier>article</identifier>
        </article>
    </frontName>

    <!-- ... other configurations ... -->

    <frontend>
        <router>
            <route>
                <identifier>article</identifier>
                <module>article</module>
                <area>frontend</area>
                <class>Magento\Framework\App\FrontController</class>
                <routes>
                    <route id="article" frontName="article">
                        <module name="article" />
                    </route>
                </routes>
            </route>
        </router>
    </frontend>

    <!-- ... other configurations ... -->
</config>
  1. Save the file and refresh your cache.

After making these changes, Magento will recognize “article” as a valid front name for your module, allowing you to access your controller actions directly without including the controller name in the URL.

Approach 2: Modifying the .htaccess File

If you prefer to handle URL rewriting at the server level using the .htaccess file, you can achieve this by adding a new rule:

  1. Open or create the .htaccess file located in your Magento root directory.

  2. Add the following line below the existing RewriteEngine On statement:

RewriteRule ^article/([^/]+)/?$ index.php?controller=$1
  1. Save the file and refresh your cache.

This rule will match any URL that starts with “article/” followed by a controller name (e.g., “article/archives”). It then rewrites this to the default Magento routing mechanism, effectively removing the need for the controller name in the URL.

Conclusion

In conclusion, rewriting URLs in Magento requires understanding how the framework’s routing system works. By leveraging Zend Routing or directly modifying the .htaccess file, you can achieve cleaner, more SEO-friendly URLs without having to move actions between controllers.

Remember that any changes to your Magento configuration files may require refreshing the cache for them to take effect. Always test your URL rewriting thoroughly to ensure it doesn’t conflict with existing rules or break functionality.

To learn more about advanced Magento development techniques and best practices, be sure to check out our comprehensive guide on Magento Development Best Practices at LaravelCompany.

Don’t hesitate to reach out if you have any other questions or need further assistance with your Magento project. Happy coding!

Extensions Configuration SEO Errors

Our network