Add Trailing Slash to URL in Magento2 Without mod_rewrite
In Magento2, you can add a trailing slash to your URLs without using Apache’s mod_rewrite module by utilizing the built-in URL rewrite functionality and event observers. This approach allows for clean, SEO-friendly URLs while keeping your server configuration simple.
The Problem: Missing Trailing Slash
By default, Magento2 may not automatically add a trailing slash (/) to some of its URLs, which can lead to duplicated content issues in search engines. While mod_rewrite offers a straightforward solution through .htaccess rules, it requires Apache and might be less portable for other environments like Nginx.
The Solution: Magento2 Core Functionality
Magento2 provides the Mage_Core_Model_Url class which handles URL generation and redirection. You can use this class to create an observer that triggers on controller action predispatch, checking if there is a trailing slash missing and redirecting accordingly.
Implementing the Trailing Slash Observer
To add a trailing slash without using mod_rewrite, follow these steps:
- Create a new module if you don’t have one already.
- Create an event observer file named
RewriteTrailingSlash.phpin your module’setc/eventsfolder.
Here is the code for the RewriteTrailingSlash.php file:
<?php
namespace LaravelCompany\TrailingSlash\Observer;
class RewriteTrailingSlash implements \Magento\Framework\Event\ObserverInterface
{
protected $_url;
protected $_redirectFactory;
public function __construct(
\Magento\Framework\Url $url,
\Magento\Framework\App\Response\RedirectInterface $redirectFactory
) {
$this->_url = $url;
$this->_redirectFactory = $redirectFactory;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$controllerAction = $observer->getControllerAction();
$request = $controllerAction->getRequest();
if (strpos($request->getPathInfo(), '.php') === false) {
return;
}
// Check for trailing slash
if ($request->getPathInfo() != rtrim($request->getPathInfo(), '/')) {
// Redirect with 301 status and append trailing slash
$redirect = $this->_redirectFactory->create();
$redirect->setHttpResponseCode(301);
$redirect->redirect($this->_url->getUrl($request->getPathInfo() . '/'));
return;
}
}
}
- Declare the observer in your module’s
etc/events.xmlfile:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch">
<observer name="trailing_slash_rewrite" instance="LaravelCompany\TrailingSlash\Observer\RewriteTrailingSlash"/>
</event>
</config>
Conclusion
Adding a trailing slash to your Magento2 URLs without using mod_rewrite is possible through the core URL rewrite functionality and event observers. The provided observer solution triggers on controller action predispatch, checks for missing trailing slashes, and redirects accordingly.
This approach keeps your server configuration clean while maintaining SEO-friendly URLs within the Magento platform itself. It also ensures that you can easily migrate or deploy your store to other environments without worrying about Apache-specific configurations.
Remember to replace LaravelCompany with your actual vendor name when implementing this solution in your own project.
For more advanced customizations and best practices in Magento2 development, check out our Magento2 Development Guide for comprehensive insights from seasoned developers.