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:
-
You added a rewrite condition and rule to your
.htaccessfile that redirectsexample.comtowww.example.com. -
You changed the base URL for both secure (HTTPS) and unsecure (HTTP) connections in the Magento backend from
http://example.comtohttp://www.example.com. -
When a user visits
www.example.com, the rewrite rule doesn’t apply, so no redirection occurs. -
However, when you try to access either
www.example.comorexample.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:
- Unsecure Base Link: http://www.example.com
- Secure Base Link: https://www.example.com
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.comtowww.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:
- Log in as an administrator.
- Click on “System” > “Cache Management”.
- 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 towww.example.comwith 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!