Magento: Creating Custom URLs Based on Product Attributes
As an e-commerce developer, you’re often required to create dynamic URLs that provide intuitive navigation for your customers while also optimizing SEO performance. In this post, we’ll guide you through the process of creating custom URLs in Magento based on product attributes like Manufacturers or Designers.
Understanding Magento’s URL Structure
Before diving into attribute-based URLs, let’s briefly review Magento’s default URL structure:
{{base_url}}/category/product-name.html
The category and product-name parts are derived from the category path and the product’s name, respectively. However, when dealing with attributes like “Manufacturers” or “Designers,” you’ll want to create more meaningful URLs.
Defining Your Custom URL Structure
For this example, let’s assume we have a “Designer” attribute set up in our Magento store, with values ranging from 0 to n. Our goal is to create the following custom URLs:
{{base_url}}/designers/some-designer-name/
which should map internally as:
{{base_url}}/?designer=136
Step 1: Create a Custom Controller
First, you need to create a new controller that will handle requests coming from your custom URL. In your app/code/local folder under your module name (e.g., Mage_YourModule), create the following directory structure:
app/code/local/YourCompany/YourModule/controllers
Inside this controllers folder, create a file called DesignersController.php with the following content:
<?php
class YourCompany_YourModule_DesignersController extends Mage_Core_Controller_Front_Action {
public function indexAction() {
$designerId = (int) $this->getRequest()->getParam('designer');
if ($designerId > 0) {
// Load designer attribute and redirect to the desired page
$designers = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('manufacturer', array('eq' => $designerId));
if (count($designers)) {
$this->_redirect('your-module/your-pagename', array('_current' => true));
}
}
Mage::getSingleton('core/session')->addError('No designer found.');
$this->_redirectReferer();
}
}
This controller handles the designers route and uses the provided designer ID to filter products by the “Manufacturer” attribute. It then redirects to a specific page within your module.
Step 2: Create Custom Rewrites
Next, you need to create custom rewrites in your module’s config.xml file:
<rewrite>
<your-module_designers>
<from><![CDATA[/designers/([0-9]+)/]]></from>
<to>yourmodule/designers/index/designer/$1</to>
</your-module_designers>
</rewrite>
This rewrite rule maps requests like /designers/136/ to the your-module/designers/index action with a $1 parameter for the designer ID.
Step 3: Update Your Theme Templates
Finally, update your theme templates (e.g., category.phtml) to include links generated using the following syntax:
<?php $designer = Mage::registry('product')->getResource()->getAttributeRawValue('manufacturer', $product); ?>
<a href="{{base_url}}/designers/{{$designer}}.html">Visit Designer Page</a>
This code retrieves the designer attribute value and uses it to create a link to the custom URL.
Conclusion
Creating custom URLs based on product attributes in Magento requires a combination of custom controllers, rewrites, and template updates. By following this approach, you can provide your customers with intuitive navigation while optimizing SEO performance for your e-commerce store.
Don’t forget to check out the official Magento Developer Documentation for more advanced topics on custom URL management and attribute-based routing in Magento.
At Laravel Company (https://laravelcompany.com), we specialize in providing tailored development solutions for e-commerce platforms like Magento. Contact us today to discuss your specific requirements and let our team of experienced developers help you optimize your online store’s performance and user experience.