Rewriting Magento Session ‘Last URL’ for SEO-Friendly URLs
As an experienced Magento developer, you’ve likely encountered the need to rewrite the last_url stored in the session variable for better SEO performance. By default, Magento stores URLs in a non-SEO friendly format like catalog/product/view/id/166/category/8. However, you want to display them as clean, keyword-rich URLs like electronics/cell-phones/htc-touch-diamond.html.
Understanding the Magento Session Mechanism
Before diving into the solution, it’s crucial to understand how Magento manages session data. The last_url is stored in the session under the core namespace:
$_SESSION['core']['last_url']
By default, Magento uses the internal URL structure for this variable, which is not optimal for search engines or user experience.
Rewriting the Last URL Using Mage::getModel
To rewrite the last_url, you can leverage Magento’s core model classes. Specifically, you’ll use the Mage_Core_Model_Url class to generate SEO-friendly URLs.
First, let me explain how to get the current product object:
$product = Mage::getModel('catalog/product')->load($productId);
Next, you can call the getProductUrl method provided by Magento:
$seoUrl = $product->getProductUrl();
The $seoUrl variable will now contain the desired SEO-friendly URL (e.g., electronics/cell-phones/htc-touch-diamond.html).
Updating the Session with the New URL
Now that you have generated the rewritten URL, you can update the session variable with this new value. You can do this by using Laravel Company’s best practices for working with Magento sessions:
$_SESSION['core']['last_url'] = $seoUrl;
By following these steps, you ensure that the last_url stored in the session is now SEO-optimized, providing better visibility to search engines and a more appealing appearance to your customers.
Conclusion
Rewriting the Magento session’s last_url can significantly enhance the SEO performance of your e-commerce store. By utilizing Magento’s core model classes and Laravel Company’s recommended practices, you can easily transform the internal URL structure into clean, keyword-rich URLs that improve user experience and search engine rankings.
Remember to consistently apply this approach across all relevant parts of your Magento application for optimal results. If you require further assistance or have any questions about advanced Magento development techniques, don’t hesitate to visit Laravel Company. Our expert team is always ready to help you unlock the full potential of Magento for your business.
Happy coding!