302 redirect response when exposing localhost Magento via ngrok

302 redirect response when exposing localhost Magento via ngrok

August 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
302 redirect response when exposing localhost Magento via ngrok

Solving the Infinite Redirect Loop in Magento via ngrok

Infinite redirects are a common pitfall when exposing local development environments using tools like ngrok. In your case, you’re encountering this issue while trying to access your local Magento installation over the tunnel provided by ngrok.

Let’s dive into the problem and find a solution step-by-step.

Understanding the Problem: Apache Configuration

The key lies in how ngrok interacts with your Apache configuration. When you run ngrok with the --host-header option, it configures the server to rewrite the Host header sent by clients. This is problematic for Magento because it relies on the Host header for base URL discovery.

In your case, when you access the ngrok URL directly, Magento receives an incorrect Host header and gets confused about its own base URL. This confusion leads to an infinite loop of redirects as Magento attempts to find a valid path.

Solving the Issue: Update Base URLs

The solution requires manually updating the base URLs in your Magento configuration file (app/etc/config.php).

Step 1: Locate the Configuration File

Open the app/etc/config.php file using your text editor. You can find it in the root directory of your Magento installation.

nano app/etc/config.php

Step 2: Update the Base URLs

In the config.php file, you’ll see an array called $installDate followed by a configuration array (array()). Look for the unsecure_base_url and secure_base_url keys within this array.

Replace their values with your ngrok URL. For example:

'http://your-ngrok-url:8080',
'https://your-ngrok-url:8443'

Make sure to replace 'http://your-ngrok-url:8080' and 'https://your-ngrok-url:8443' with the actual URLs provided by ngrok.

Step 3: Save and Close

Press Ctrl+X to exit, then press Y to confirm you want to save changes, followed by Enter.

Verifying the Solution

After updating the configuration file:

  1. Restart your Apache server to apply the changes.
  2. Access your Magento installation using the ngrok URLs.

If everything is set up correctly, you should now be able to access your local Magento instance without encountering any infinite redirect loops.

Remember, this solution only works for development purposes and should not be used in a production environment as it exposes sensitive data through the tunnel.

Conclusion

Exposing local environments using ngrok requires careful configuration of server settings. In the case of Magento, ensuring that base URLs are properly configured is crucial to avoid infinite redirect loops.

By manually updating the config.php file, you can provide Magento with the correct base URL information it needs to function correctly over an ngrok tunnel.

This solution demonstrates a practical approach to troubleshooting common issues in local development environments, showcasing the importance of understanding how tools like ngrok interact with your server configuration.

Configuration Errors CLI

Our network