Magento Filters URL Rewrite: A Comprehensive Guide
Hello there! I hope this post finds you well. Today, let’s dive into an interesting challenge that many Magento developers face - the need to create SEO-friendly URLs for product filters.
The Challenge: Current State of Filter Links
First, let me set the stage. Imagine a Magento-powered e-commerce site, where we want to optimize the URL structure for layered navigation filters:
https://mymagazine.ru/category/subcategory1?manufacturer=1926
As you can see, the manufacturer filter is appended as a GET parameter to the end of the URL. While this works from a functional perspective, it’s far from ideal for SEO purposes.
The Goal: Creating Clean, Aliased URLs
Our aim here is to transform these clunky URLs into something more elegant and friendly:
https://mymagazine.ru/category/subcategory1/my_manufacturer
In this structure, “my_manufacturer” would act as an alias for the GET parameter “?manufacturer=1926”. The idea is to make the URL cleaner, easier to read, and more memorable.
Manual Approach: The Tedious Path
Before we jump into solutions, let’s briefly consider a manual approach. In Magento, you can manually create SEO-friendly URLs using the admin panel:
- Go to Catalog > Manage Categories.
- Click on a category and switch to the URL Key field.
- Enter your desired alias (e.g., my_manufacturer).
While this works for small sites, it becomes an administrative nightmare as your product catalog grows. For large e-commerce platforms with thousands of products across hundreds of categories, manual URL creation isn’t scalable.
Extensions: The Path We Didn’t Take
You mentioned two extensions that you found in your search:
While these extensions offer powerful features, they didn’t meet your specific needs in this context.
The Solution: Custom URL Rewrite
Now, let’s roll up our sleeves and tackle the problem head-on. To achieve SEO-friendly URLs for layered navigation filters, we can use Magento’s built-in URL rewrite functionality with a bit of custom development.
Here are the high-level steps:
- Create a new module: Use a tool like Composer to create a new Magento 2 module that will handle our URL rewrites.
- Override the Layered Navigation block: In your module, override the
Magento\LayeredNavigation\Block\Itemclass. - Generate and save the URL rewrite: When a filter is applied, generate the desired SEO-friendly URL using the alias you’ve set up in your Magento admin.
- Set up the URL rewrite in the event observer: Create an observer that listens for the
controller_action_predispatch_catalog_category_viewevent. - Save the URL rewrite to the database: In your observer, check if a new URL should be created. If so, save it using Magento’s URL rewrite model.
Code Example: Creating the URL Rewrite
Let me illustrate this with a simple example of how you might generate and save the URL rewrite:
public function execute(Observer $observer)
{
// Get the current category and filter data
$category = $observer->getControllerAction()->getCategory();
$filterData = $observer->getEvent()->getFilter();
// Check if a new URL needs to be created
if (isset($filterData['manufacturer'])) {
// Generate the SEO-friendly URL
$alias = 'my_manufacturer';
$urlRewrite = sprintf('%s/%s', $category->getUrlPath(), $alias);
// Save the URL rewrite
$model = $this->_objectManager->create('Magento\UrlRewrite\Model\UrlRewriteFactory');
$model->setStoreId($storeId)
->setCategoryCategoryId($category->getId())
->setRequestPath($urlRewrite)
->save();
}
}
Backlink: Dive Deeper with Laravel Company
If you’re looking to learn more about Magento development and optimization, I highly recommend checking out the resources available at Laravel Company. They offer a wealth of tutorials, guides, and tips for getting the most out of your e-commerce platform.
Conclusion: SEO-Friendly URLs with Custom Development
In conclusion, while there are extensions that can help optimize certain aspects of Magento, creating custom URL rewrites often requires a tailored approach. By leveraging Magento’s built-in functionality and some custom development, we can achieve clean, optimized URLs for our layered navigation filters.
The key to success lies in understanding the underlying architecture, using events and observers effectively, and leveraging Magento’s core models and repositories. With a bit of creativity and careful planning, you can turn those clunky GET parameters into elegant SEO-friendly aliases that enhance the user experience and boost your site’s visibility.
Remember, the devil is often in the details when it comes to e-commerce development. By paying close attention to URL structure and optimization, you’ll be well on your way to building a robust, SEO-friendly Magento store.
Happy coding!