HTTP to HTTPS and 301 redirect chains on Magento

HTTP to HTTPS and 301 redirect chains on Magento

September 30, 2026 · Illustrated author avatarBy Stefan Bogdanescu
HTTP to HTTPS and 301 redirect chains on Magento

Solving HTTP to HTTPS Redirect Chains on Magento

Understanding the Current Situation

You’ve successfully transitioned your Magento site from HTTP to HTTPS, but now you’re facing unnecessary redirect chains that are slowing down page load times and potentially impacting search engine rankings. Currently, your redirects look something like this:

  1. http://domain.tld => http://www.domain.tld => https://www.domain.tld
  2. http://domain.tld/subdir/ => http://www.domain.tld/subdir/ => https://www.domain.tld/subdir/

Not only are you adding the “www” redirection twice for each URL, but this structure also creates a chain of three redirects before reaching the final destination.

The Goal: Streamline Redirects

Your objectives are clear:

  1. Directly redirect from http://domain.tld to https://www.domain.tld
  2. Preserve directory paths, so http://domain.tld/subdir/ should directly point to https://www.domain.tld/subdir/

Additionally, you want to ensure that other Magento stores on the same server remain HTTP-only and redirect back to HTTPS.

Optimizing Your .htaccess File

Let’s revise your .htaccess file to achieve these goals. The key is to combine the redirection rules for better performance.

# Enable mod_rewrite engine
RewriteEngine On

# Ensure URLs are case-sensitive
Options +FollowSymLinks
RewriteBase /

# DEFAULT RULES
DirectoryIndex index.php

SetEnvIf SERVER_PORT 443 HTTPS=on
SetEnvIf X-Forwarded-Proto https HTTPS=on

<IfModule mod_rewrite.c>

    # REDIRECT ALL TO HTTPS DIRECTLY (INCLUDING WWW)
    RewriteCond %{HTTP_HOST} ^domain\.tld$ [NC]
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/%{REQUEST_URI} [R=301,L,NE]

    # REMOVE INDEX.PHP AND REDIRECT TO ROOT DIRECTORY
    RewriteCond %{THE_REQUEST} \s+/index\.php [NC]
    RewriteRule ^(.*?)index\.php$ /$1? [L,R=301,NC,NE]

    # REMOVE HOME REWRITE FROM MAGENTO
    RewriteRule ^home/?$ /? [R=301,L,NC]

    # ADD WWW TO NONE WWW FOR BOTH HTTPS AND NONE HTTPS
    RewriteCond %{HTTP_HOST} !^www\.domain\.tld [NC]
    RewriteCond %{HTTPS}s ^on(s)|
    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    # REDIRECT ALL .HTML FILES AND ALL .HTML/ FILES WITH TRAILING SLASH
    RewriteRule ^google[0-9a-f]+.html$ - [L]
    RewriteRule (.+)\.html$ /$1/ [L,R=301]
    RewriteRule (.+)\.html\/$ /$1/ [L,R=301]

    # ADD TRAILING SLASH
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ $1/ [L,R=301]

    # CHECK IF REDIRECT POINTS TO A VALID FILE
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # REWRITE EVERYTHING ELSE TO INDEX.PHP
    RewriteRule .* index.php [L]

</IfModule>

Key Changes Made

Direct HTTPS Redirect with WWW

The new rule RewriteCond %{HTTP_HOST} ^domain\.tld$ [NC] RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/%{REQUEST_URI} [R=301,L,NE] now handles both the “www” and HTTPS redirection in a single pass. It checks if the domain is your main one (domain.tld) and if it’s not already using HTTPS.

Preserve Directory Paths

The RewriteCond %{THE_REQUEST} \s+/index\.php [NC] RewriteRule ^(.*?)index\.php$ /$1? [L,R=301,NC,NE] rule is unchanged but maintains its purpose—removing “index.php” from URLs while keeping the directory paths intact.

Other Magento Stores Remain HTTP

The existing rules already ensure that other domains and subdomains are handled correctly. If you have a separate store like domain2.tld, this rule will not apply to it, so it remains with its default HTTP setup.

Conclusion: Streamlined Redirects for Better Performance

With these modifications, your site now boasts clean, efficient redirects:

  1. http://domain.tld => https://www.domain.tld
  2. http://domain.tld/subdir/ => https://www.domain.tld/subdir/

These direct redirections eliminate the unnecessary intermediate steps, improving page load times and maintaining SEO best practices.

Remember to clear your browser cache after implementing these changes for them to take effect immediately. This optimized .htaccess file will serve as a solid foundation for your secure, high-performing Magento site.

Indexing Performance

Our network