Magento redirect loop after Base URL and htaccess change

Magento redirect loop after Base URL and htaccess change

July 31, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento redirect loop after Base URL and htaccess change

Solving Magento Redirect Loop After Base URL and .htaccess Change

The Challenge

You’ve recently installed Magento 1.9 and are trying to enforce www usage for your users. You followed the instructions on SiteGround’s KB, adding two lines to your .htaccess file and changing the base URL in the Magento backend. However, now you’re stuck with a redirect loop error.

Understanding the Issue

The problem lies in the combination of changes you made:

  1. You added a rewrite condition and rule to your .htaccess file that redirects example.com to www.example.com.

  2. You changed the base URL for both secure (HTTPS) and unsecure (HTTP) connections in the Magento backend from http://example.com to http://www.example.com.

  3. When a user visits www.example.com, the rewrite rule doesn’t apply, so no redirection occurs.

  4. However, when you try to access either www.example.com or example.com, the system gets stuck in an infinite loop because it’s trying to redirect between these two URLs that are now both valid according to your changes.

The Solution

To resolve this issue, we need to update our approach:

Step 1: Update the Magento Base URL (Again)

Return to your Magento backend at system > config > web. Ensure you have:

Save these changes.

Step 2: Modify the .htaccess Rewrite Rule

Open up your .htaccess file located in the public_html folder of your Magento install and update the rewrite rule as follows:

## Enable rewrites
Options +FollowSymlinks
RewriteEngine On

# Redirect non-www to www
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

# Force HTTPS for secure pages
RewriteCond %{HTTPS} off
RewriteRule ^(?!admin)(.*) https://%{HTTP_HOST}/$1 [R=301,L]

Here’s what the updated code does:

  • We’re still redirecting example.com to www.example.com. However, we’ve added the [L] flag which means “last rule”. This prevents further processing of the URI if this rule matches.

  • The second rewrite rule is new. It checks if the connection is not secure (HTTP), then it redirects all non-admin pages to their HTTPS equivalents.

Step 3: Clear Cache

After updating your .htaccess file, clear any cached versions:

  1. Log in as an administrator.
  2. Click on “System” > “Cache Management”.
  3. Select “Flush Magento Cache”.

Verifying the Solution

  • Visit www.example.com: It should load successfully without a redirect loop.

  • Visit example.com: You’ll be redirected to www.example.com with no errors.

Remember, these changes are only effective after you’ve updated your .htaccess file and cleared Magento’s cache. If you encounter any further issues, feel free to explore our Magento troubleshooting guides for more in-depth solutions.

Conclusion

By strategically modifying the base URL settings in your Magento backend and adjusting the rewrite rules in your .htaccess file, you can successfully enforce www usage without getting stuck in redirect loops. Always ensure that your changes are consistent across both secure and unsecure URLs to avoid conflicts.

Happy coding!

Magento 1 Admin Configuration Errors

Our network