Magento: Clearing All Permanent Redirects
Introduction
When developing a Magento site, it’s common to encounter unwanted permanent redirects that can hinder your development process. In this post, we’ll guide you through clearing all these redirects from scratch and starting with a clean slate for URL management.
Understanding the Problem
Permanent redirects in Magento are managed under Catalog -> URL Rewrite Management. These redirects are created when products are uploaded or URLs are changed. If not handled correctly, they can lead to 404 Not Found errors and broken links.
In your case, enabling “Create permanent redirects” during product upload via a feed led to incorrect URLs being permanently redirected. Even after re-uploading the corrected data, the old redirects persisted, causing conflicts with new URLs.
Clearing All Permanent Redirects
To completely clear all permanent redirects in Magento:
Step 1: Delete Existing Entries
First, delete any existing URL rewrites from Catalog -> URL Rewrite Management.
- Select all entries in the grid.
- Click on “Delete” in the top right corner.
Note that this will only remove the entries from your database but won’t actually clear the redirects.
Step 2: Clear Magento Cache
Clear all cache types from System -> Cache Management. This includes:
- Config
- Layout
- Block HTML output
- Transient variables
- EAV types
- Customer notifications
- Menu and config
- Session data
- Full page cache
Click the “Flush Magento Cache” button.
Step 3: Flush URLs
Finally, flush all URL rewrites using a script. This will permanently clear all redirects from the database:
- Create a file called
flushurls.phpin your Magento root directory. - Add the following PHP code to this file:
<?php
require_once 'app/Mage.php';
Mage::init();
$model = Mage::getModel('core/url_rewrite');
$collection = $model->getCollection();
foreach ($collection as $item) {
$model->delete($item);
}
echo "All URL rewrites deleted.";
?>
- Run this script in your terminal:
php flushurls.php
Verifying the Results
After running the flushurls.php script:
- Check if all redirect entries are gone from the
core_url_rewritetable in your database. - Verify that any broken URLs previously returning 404 errors now load the correct content.
Best Practices for Avoiding Redirects
To avoid encountering this issue in the future:
- Disable “Create permanent redirects” under
System -> Config -> Catalog -> SEOunless absolutely necessary. - Always handle URL changes manually using the Magento admin interface or API to maintain full control over your site’s URLs.
- Regularly monitor and clean up any accidental redirects that may occur during development.
Conclusion
Clearing all permanent redirects in Magento is a straightforward process once you understand the underlying mechanisms. By deleting existing entries, clearing cache, and flushing URLs using a script, you can start with a clean slate for URL management on your developing site.
Remember to always be cautious when enabling automatic redirect creation, as it can lead to unintended consequences if not managed properly. With this guide, you now have the tools to maintain a healthy Magento URL structure throughout your development cycle.
For more in-depth guidance on Magento SEO best practices and advanced URL handling techniques, consider exploring our comprehensive Magento developer resources at LaravelCompany.