Changing Base URL in Magento: A Comprehensive Guide
If you’re facing a 404 Not Found error or your entire Magento site has gone offline after changing the base URL through the admin panel, don’t panic. We’ll walk you through the steps to safely modify the base URL and resolve the issue.
Understanding the Base_URL
The base_url is a critical configuration setting in Magento that defines the primary domain for your store’s frontend and backend. It includes the protocol (http or https), subdomain, domain name, and path if any.
When you change this value through the admin panel or via your database, Magento needs to update several places to reflect these changes:
- Configuration file (
app/etc/local.xml) - Core_Config_Data table in your database
- Session data
- Cache
Step-by-Step: Changing Base_URL
1. Update the configuration file
Firstly, you need to edit the base_url value in the local.xml file located at app/etc/.
<config>
<global>
<!-- Other configurations... -->
<web>
<unsecure>
<base_url>http://example.com</base_url>
</unsecure>
<secure>
<base_url>https://example.com/</secure>
</secure>
</web>
</global>
</config>
Make sure to replace http://example.com and https://example.com/ with your new base URL. Save the file.
2. Update the database
Next, you need to update the core_config_data table in your Magento database.
UPDATE core_config_data
SET value = 'http://new-url.com'
WHERE path = 'web/unsecure/base_url';
UPDATE core_config_data
SET value = 'https://new-url.com/'
WHERE path = 'web/secure/base_url';
Replace http://new-url.com and https://new-url.com/ with your new base URLs.
3. Clear cache
Magento stores various data in cache to improve performance. Clearing the cache will ensure that all updated configurations are loaded:
php bin/magento cache:flush
Or, if you’re using a version of Magento prior to 2.4.x:
rm -rf var/cache/*
rm -rf generated/code/
chmod u+w var/
4. Update session data
If your site is live and has active user sessions, you’ll need to update the core_session table as well:
UPDATE core_session
SET session_data = REPLACE(session_data, 'http://old-url.com', 'http://new-url.com');
Troubleshooting Common Issues
Issue: Admin panel still shows old URL
If your admin panel is still showing and linking to the old base URL, you might need to clear the admin cache as well:
php bin/magento c:f --adminhtml
Or for older versions of Magento:
rm -rf var/cache/adminhtml/*
rm -rf generated/code/adminhtml/
chmod u+w var/
Issue: 404 error on secure URL
If you’re using HTTPS and receiving a 404 Not Found error, make sure your base URL includes the trailing slash (/) for the secure URL:
<secure>
<base_url>https://example.com/</secure>
</web>
Conclusion
Changing the base URL in Magento requires careful attention to detail and a systematic approach. By following our guide, you should be able to safely modify your site’s primary domain without losing data or causing downtime.
Remember, always backup your database before making any changes to your Magento configuration files or database tables.
If you’re still facing issues after trying these steps, consider reaching out to a professional Magento developer for assistance. We’d be happy to help you troubleshoot further at Laravel Company.