Solving Magento’s Automatic localhost Redirection on Live Server
Introduction
If you’ve recently migrated your Magento store from localhost to a live server but are now facing automatic redirects back to the development environment, you’re not alone. This issue is common and can be quite frustrating. In this post, we’ll walk through a step-by-step guide to resolve this redirection problem and ensure your Magento site runs smoothly on the production server.
Understanding the Issue
The automatic localhost redirection occurs because of conflicts between the base URLs configured in your Magento installation. When you migrate from a local development environment to an online one, these URLs need to be updated accordingly for proper functioning.
Step 1: Verifying Base URL Configuration
First, let’s ensure that the base URLs are correctly set up in your database. You can check this by running the following SQL queries:
SELECT * FROM core_config_data WHERE path LIKE 'web/unsecure/base_url%';
SELECT * FROM core_config_data WHERE path LIKE 'web/secure/base_url%';
These queries will display the current base URLs for both unsecured and secure connections. Make sure they are pointing to your live domain and not localhost.
Step 2: Updating Base URL in Admin Panel
Next, update the base URLs directly from your Magento admin panel:
- Log into your Magento admin dashboard.
- Navigate to Stores > Configuration > General > Web.
- Look for the ‘Base URLs’ section.
- Ensure that both ‘Unsecure Base URL’ and ‘Secure Base URL’ are set to your live domain (e.g.,
http://unbeatablewatches.comorhttps://unbeatablewatches.com).
Step 3: Clearing Cache
Clearing the cache is crucial after updating the base URLs. You can do this manually through the admin panel:
- Go to System > Cache Management.
- Select all cache types (or choose ‘All Types’).
- Click on ‘Flush Magento Cache’.
Alternatively, you can use a command-line tool like php bin/magento cache:flush for better control and automation.
Step 4: Flushing the Opcode Cache
If your server uses an opcode cache like APC or Opcache, make sure to flush it as well. This step ensures that the updated Magento code is picked up correctly by the server.
For Apache-based servers with APC enabled:
apc_clear_cache;
For Nginx-based servers with Opcache enabled:
opcache_reset;
Step 5: Flushing DNS Cache
Sometimes, the issue persists due to cached DNS entries on your local machine or the server itself. To flush the DNS cache:
- On Ubuntu/Debian:
sudo systemd-resolve --flush-caches
- On CentOS/Fedora:
sudo dnsmasq --clear-hosts
Conclusion
Resolving Magento’s automatic localhost redirection requires a combination of database updates, admin panel configuration adjustments, and cache clearing. By following the steps outlined above, you should be able to eliminate this issue and ensure your Magento site operates correctly on the live server.
Remember, it is always best practice to test your store thoroughly after making significant changes or migrations. This includes checking both frontend visibility and backend functionality to guarantee a seamless user experience.
For more in-depth guides on Magento development and migration strategies, check out our comprehensive resources at LaravelCompany.