Magento - URL rewriting or redirection issue

Magento - URL rewriting or redirection issue

September 16, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - URL rewriting or redirection issue

Magento URL Rewriting or Redirection Issue: A Comprehensive Solution

As an experienced developer specializing in e-commerce platforms like Magento, I often encounter issues related to URL rewriting and redirection, especially when dealing with SSL-enabled sites. Let’s dive into the problem described and provide a step-by-step solution.

Understanding the Problem

The user is facing a peculiar issue where their Magento site automatically redirects from the admin login page (sitename.com/admin) to the homepage (https://sitename.com) on the first access after clearing browser cache or in private mode. This redirection happens for both the admin panel and inner pages of the site.

Additionally, when using a third-party extension like “Login and Pay with Amazon,” an error occurs during the login process: “No data received” on Chrome and “Secure connection failed” on Firefox.

Analyzing the .htaccess File

The user has provided their current .htaccess file content. Let’s analyze this file to identify any potential issues:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.sitename.com/$1 [R,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

Identifying the Root Cause

The main issue seems to stem from the following lines in the .htaccess file:

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.sitename.com/$1 [R,L]

These rules force the site to redirect from the non-secure to secure version and also enforce the use of www. However, the issue lies in how Magento handles these redirects internally.

Resolving the Issue

To resolve this problem, we need to make some modifications to both the .htaccess file and the Magento configuration.

Step 1: Update the .htaccess File

Replace the existing code with the following optimized version:

RewriteEngine On
RewriteCond %{HTTPS} off [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} ^sitename\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.sitename\.com [NC]
RewriteRule ^(.*?)/?$ https://www.sitename.com/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_FILENAME} !admin
RewriteRule .* index.php [L]

The key changes are:

  1. Removed the second RewriteCond checking for port 80, as it’s unnecessary.
  2. Added an OR condition to handle both www and non-www versions of the site.
  3. Added a negative check for admin in the request URI.

Step 2: Update Magento Configuration

Navigate to your Magento admin panel under System > Configuration > General > Web.

In the “Base URLs” section, ensure that both “Unsecured Base URL” and “Secure Base URL” are set to https://sitename.com. The presence of a trailing slash is crucial.

Step 3: Clear Cache

After making these changes, clear your browser’s cache completely. This step is essential for ensuring the updates take effect immediately.

Expected Results

Upon implementing these steps:

  1. Accessing the admin panel (sitename.com/admin) should display the admin login page without any redirection issues.
  2. The site will maintain a consistent URL structure, always using https://www.sitename.com.
  3. The “Login and Pay with Amazon” extension should work correctly, as the redirect issue has been resolved.
  4. Clearing browser cache or accessing in private mode should no longer cause automatic redirection to the homepage.

Remember that proper maintenance of your .htaccess file is essential for a well-functioning Magento site. Always test changes on a staging environment before deploying them to production.

By following these guidelines, you should be able to resolve the URL rewriting and redirection issues in your Magento store effectively. If any further assistance is needed, feel free to reach out to our team at Laravel Company.

Happy coding!

Cache Extensions Admin Errors

Our network