Removing Controller Name from URL for Custom Magento Modules
Creating custom modules in Magento requires careful configuration of your controller routing. If you’ve created an IndexController for your module, the default URL will include the controller name. In this guide, we’ll walk through how to remove the controller name from the URL and prevent that pesky 404 error.
Understanding the Problem
When you create a new module and define a controller like so:
public function indexAction()
{
// Your code here
}
The URL generated for this action will be in the format http://192.168.1.25/upload/index.php/capsync/index/api.
Fixing the 404 Error
To remove the controller name from the URL, you need to configure your module’s front controller.
Step 1: Update your config.xml file
Open your config.xml file located in the root directory of your custom module. Look for the <frontName> tag inside the <global> section:
<global>
<models>
<capsync> <!â Your namespace â>
<class>Capsync_Model</class>
<resourceModel>capsync_resource</resourceModel>
</capsync>
</models>
<resources>
<capsync_setup>
<setup>
<module>Capsync_Setup</module>
</setup>
</capsync_setup>
</resources>
<frontName>
<capsync> <!â Your module name â>
<area>adminhtml</area>
</capsync>
</frontName>
</global>
Change the <frontName> tag to match your module name:
<frontName>
<capsync>
<area>adminhtml</area>
</capsync>
</frontName>
Step 2: Clear Your Cache
After updating the config.xml file, you must clear Magento’s cache for the changes to take effect. You can do this manually through the Admin area or use a script.
The Final URL
Once you’ve cleared the cache, visit your homepage again. The new URL should now look like http://192.168.1.25/upload/index.php/capsync/api, with the controller name removed.
Remember that this method will only work for frontend URLs. To remove the controller name from admin URLs as well, you’ll need to modify your admin area configuration separately.
Conclusion
Removing the controller name from Magento URLs involves a simple yet crucial update in your module’s config.xml file. By ensuring your frontName matches your module name and clearing the cache afterward, you can achieve shorter, cleaner URLs for your custom modules.
For more advanced routing configurations or to learn about creating custom URL rewrites in Magento, check out our detailed guide on URL Rewriting in Magento.
This approach will help you maintain clean and SEO-friendly URLs for your custom Magento extensions.