Solving Magento 2 Admin Portal ERR_TOO_MANY_REDIRECTS in Staging Environment
Setting up a staging environment for your Magento 2 site is a crucial part of the development workflow, allowing you to test updates, security patches, and new features before deploying them to production. However, migrating to a new server or IP address can sometimes lead to issues with the admin portal, resulting in an ERR_TOO_MANY_REDIRECTS error.
Understanding the Problem
The issue arises when the Magento admin portal is unable to correctly redirect due to changes in the base URL or conflicts between secure and non-secure cookie settings. This typically happens after you’ve cloned your database and updated the base_url value.
Here’s a simplified version of what you’re experiencing:
- You clone the database from your test environment.
- Update the
base_urlfor the staging environment usingbin/magento setup:store-config:set. - Run various commands to upgrade, deploy static content, and reindex the indexers.
- The admin portal enters a redirect loop with a 302 status.
The resulting URL looks something like this:
http://{ip-staging}/admin/http:/admin/admin/index/index/key/f3327bb999d2fa7577a5e2817a09628fe74cxdsca46e06c82cf690f221004d7d/
Solving the Redirect Loop
To resolve this issue, you need to ensure that your Magento configuration is consistent across the secure and non-secure contexts.
Step 1: Set Use in AdminHTML Flag
The use_in_adminhtml flag determines whether to use Secure URLs for the admin area. Run the following command to set it to ‘0’:
bin/magento config:set web/secure/use_in_adminhtml 0
This tells Magento not to enforce secure URLs for the admin interface.
Step 2: Flush Cache
After making configuration changes, you should always flush the cache to ensure the new settings take effect:
bin/magento cache:flush
Step 3: Adjust Cookie Settings (Optional)
If the issue persists after setting use_in_adminhtml and flushing the cache, you can try disabling the HTTPOnly flag for admin cookies. However, this is generally not recommended due to security implications:
bin/magento config:set web/cookie/cookie_httponly 0
Automating the Process
To automate this process without manually opening the admin portal and running cache flush commands, you can create a script that performs these steps sequentially.
Here’s an example of how to create a bash script named fix-admin-redirect.sh in your project root:
#!/bin/bash
echo "Fixing Magento Admin Redirect Loop..."
# Step 1: Set Use in AdminHTML Flag
bin/magento config:set web/secure/use_in_adminhtml 0
# Step 2: Flush Cache
bin/magento cache:flush
echo "Admin redirect loop fixed successfully."
Make sure to give the script executable permissions:
chmod +x fix-admin-redirect.sh
Now, you can run this script after migrating your data and updating the base_url as part of your deployment process.
Conclusion
Setting up a staging environment for Magento 2 requires careful attention to details such as base URLs and cookie settings. By following these steps and creating an automated script to fix the admin redirect loop, you can significantly streamline your development workflow and minimize manual intervention.
Remember that security is paramount in e-commerce platforms like Magento. Always ensure that your configuration changes are necessary and understand their implications before deploying them to production.
For more insights into Magento 2 best practices and advanced configuration techniques, consider visiting Laravel Company.