25th Anniversary Savings | 25% Off Dedicated Servers*Shop Now
25th Anniversary Savings | 25% Off VPS Hosting* †††Shop Now
Limited Inventory: High-Performance AMD-Powered Servers Now Available.* Shop Now >
Dedicated Hosting Deals | From $99/moShop Now

Troubleshooting: Too Many Redirects

Reading Time: 7 minutes

A redirect sends traffic from one URL to another. When moving content from one page to another or changing the permalink structure of a website, redirects are helpful. You can read this article to learn more about the Too Many Redirects error, including what causes it and how to fix it.

What Does the Too Many Redirects Error Mean?

Your browser displays the Too Many Redirects error when a requested web page cannot load because it must follow too many redirects to receive the content from the server. This redirect loop often results from competing redirects, one trying to force HTTPS (via SSL) and another redirecting back to HTTP (non-SSL) or between www and non-www forms of the URL. 

Why Does the Too Many Redirects Error Occur?

Several things can cause the Too Many Redirects error. The most frequent cause is a misconfigured setting in the .htaccess file. Conflicting redirects on the server side or a CDN misconfiguration are frequent causes. Other potential causes include corrupted or infected WordPress plugins and themes, corrupted or infected browser extensions, incorrect DNS settings, incorrect SSL/TLS certificate configuration, improper domain migration, and conflicts with third-party services.

Your Browser

To protect from the Too Many Redirects error, your browser typically only permits a maximum of 10 redirects before giving up and displaying the error message. This message appears differently in Firefox, Chrome, and other browsers.

Firefox

Chrome

Cache and Cookies

If you've recently made changes to your website, they may not be reflected immediately due to cached data. Clearing your browser's cache and cookies is the first step in testing.

Using Developer Tools for Redirect Loops

On Windows and Linux, you can access the Developer Tools menu in your browser one of two ways. Either choose Tools > Web Developer or the keyboard shortcut Ctrl + Shift + I or F12. On Mac OS X, access the developer tools on the Mac desktop by pressing Cmd + Opt + I on your keyboard. Make sure the Network tab is selected in one of these, and then reload the problematic page.

Developer Tools in Firefox

If you use keyboard shortcuts or the corresponding menu items to open the developer tools in Firefox, you will see the dashboard exactly as it appears in the screenshot.

Developer Tools in Chrome

If you use keyboard shortcuts or the corresponding menu items to open the developer tools in Chrome, you will see the dashboard exactly as it appears in the screenshot.

Using cURL for Redirect Loops

The cURL command line utility allows you to send virtually any HTTP request from the command line. This feature is useful for various tasks, including uploading data to an API and downloading files.

You can use the script below to test your website using curl.

#!/bin/bash
 echo
 for yourdomain in $@; do
 echo --------------------
 echo $yourdomain
 echo --------------------
 curl -sILk $yourdomain | egrep 'HTTP|Loc' | sed 's/Loc/ -> Loc/g'
 echo
 done

Copy this code into a text editor and save it as redirect_checker.sh. Then run the following command to make this file executable.

chmod +x redirect_checker.sh

Here is an example of the output.

~$ ./redirects.sh sampledomain.com
--------------------
sampledomain.com
--------------------
HTTP/1.1 301 Moved Permanently
 -> Location: https://samplemydomain.com/
HTTP/2 301 
HTTP/2 200

Example of an Infinite Redirect From HTTP to HTTPS

You can also use the script we mentioned earlier to check for the Too Many Redirects error on your website.  If the website redirects too many times, you will receive the same result as shown below.

~$ ./redirects.sh www.testdomain.com
--------------------
 http://www.testdomain.com
 --------------------
 HTTP/1.1 301 Moved Permanently
 -> Location: https://www.testdomain.com/
 HTTP/1.1 301 Moved Permanently
 -> Location: http://www.testdomain.com/
 ....
 HTTP/1.1 301 Moved Permanently
 -> Location: http://www.testdomain.com/
 HTTP/1.1 301 Moved Permanently
 -> Location: https://www.testdomain.com/

A Side Note on Redirect Types

Two primary types of redirects can be used to send visitors to a different URL than what they were intended to see. The 301 and 302 redirects are the most common, but there are differences between them that you should be aware of before deciding whether to use them.

A 301 redirect is a permanent redirect and is thought to be the most effective way to redirect a web page. When a website has moved permanently to a new address, this redirect is used to direct all traffic from the old URL to the new URL. A 302 redirect serves to propagate changes to a forwarding address quickly.

Redirects in the .htaccess File

The .htaccess file is a configuration file used to alter the behavior of the Apache server for each directory. It controls how a website can be accessed, blocked, and redirected. The mod rewrite module for Apache is what enables these rewrites. Every rewrite rule is composed of a combination of rewrite condition (RewriteCond) tests and a corresponding rule (RewriteRule). You can identify redirects by using the examples of redirects that have been provided below.

Force HTTPS

The following .htaccess code determines whether the request entered the server via HTTP or HTTPS first. The configuration instructs the browser to reroute to the HTTPS version of the same website and URL if the request did not use HTTPS.

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

Force HTTPS: When Behind a Load Balancer or Proxy (CloudFlare/Incapsula/Sucuri/etc.)

It is possible to use SSL on the front end while disabling it on the back end by using a proxy, such as a load balancer, or a web firewall, such as Cloudflare, Incapsula, or Sucuri. To make this work, you must ensure that the proxy sends the original HTTPS request to the server using only HTTP.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Force Non-www

This redirect only looks to see if the website was requested with www at the beginning of the domain name. The browser is instructed to rewrite the request and go to the non-www version of the domain name if the www is present.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Force www

This redirect ensures that the URL was not requested with the www prefix. If the www is not present, it rewrites the request and instructs the browser to redirect to the www version of the domain.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

WordPress

The URL of the website is defined by the WordPress CMS as a value in the database, but it uses an .htaccess file to redirect URLs to the index.php file. The database credentials used on the site can be found in the WordPress configuration (wp-config.php). Using the grep command, you can look for these values in the wp-config.php file via SSH.

]$ grep DB wp-config.php
define( 'DB_NAME', 'example_db' );
define( 'DB_USER', 'example_du' );
define( 'DB_PASSWORD', 'dbpassword' );
define( 'DB_HOST', 'localhost' );
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');

The wp_options Table

Once you know the name of the database, you can look in the options table of the WordPress database to see the current URL setting. The home URL and site URL lines are the two crucial lines. Run the following command from the command line to find them instead of using a database management tool.

mysql -e 'show tables' database_name | grep options
prefix_options

For eg: 
]$ mysql -e 'show tables' example_db | grep options
wp_options

Checking the Configured URL

You can check the home URL and site URL lines in the options table's current values from the command line. You should receive an output from the command that looks like the example below. It's crucial that these correspond with one another and match your expectations in most situations. You should update them if they differ from what you had expected.

mysql -e 'select * from wp_options where option_name rlike "home|siteurl"' database_name
+-----------+-------------+----------------------------------+----------+
 | option_id | option_name | option_value       |       autoload        |
 +-----------+-------------+----------------------------------+----------+
 |        40 | home        | https://www.sampledomain.com        | yes |
 |         1 | siteurl     | https://www.sampledomain.com        | yes |
 +-----------+-------------+----------------------------------+----------+

Updating the Configured URL

The following command modifies the URLs in the two rows of the wp options table for the specified database. This command should work when you need to update or correct the URL for a WordPress site.

mysql -e 'update wp_options set option_value="https://www.sampledomain.com" where option_name rlike "home|siteurl"' database_name

Magento

One of the following files—local.xml or env.php—configures the Magento database name. You can also set a prefix for the table names in the Magento database, though this is typically not done. The primary configuration table of the database should therefore have the name core config data.

When following the instructions, change <Magento installation directory> to your Magento installation directory:

  • To get the Magento 1 configuration file, go to /<Magento installation directory>/app/etc/local.xml.
  • To get the Magento 2 configuration file, go to /<Magento installation directory>/app/etc/local.xml.

You can set the Magento site's base URLs using the core_config_data table. Use the following command to find the base URLs' details. You can also use a database management tool to find these details.

mysql -e 'select * from core_config_data where path rlike "base_url"' database_name  +-----------+---------+----------+-----------------------+----------------------------------------------+
 | config_id | scope   | scope_id | path             | value                                                   | +-----------+---------+----------+-----------------------+----------------------------------------------+
 |         3 | default |        0 | web/unsecure/base_url | https://www.sampledomain.com    |
 |         4 | default |        0 | web/secure/base_url   | https://www.sampledomain.com       | +-----------+---------+----------+-----------------------+----------------------------------------------+

To update the Base URLs in the Magento database, use the command shown below.

mysql -e 'update core_config_data set value="https://www.sampledomain.com" where path rlike "web/.*/base_url"' database_name

Wrapping Up

If your .htaccess redirects to a URL that does not match the content of the database, the site or URL is redirected too many times and you will get the error too many redirects. It's crucial to remember that these CMSs also provide redirection options within the site code. The tools are required to determine whether these factors cooperate or compete with one another and to take corrective action if necessary.

Troubleshooting the Too Many Redirects error can be daunting if you don’t know where to look. Fortunately, Liquid Web offers 24/7/365 support to assist fully managed server customers. If you’re ready for a new managed hosting experience, contact our sales team today to get started.

Avatar for Mohammed Noufal

About the Author: Mohammed Noufal

Mohammed Noufal has worked as a senior server administrator for 8+ years. He can be found on LinkedIn to know more or connect.

Latest Articles

What Is Umask and How to Use it Effectively

Read Article

Troubleshooting: Too Many Redirects

Read Article

What Is a Time Series Database? How It Works & Use Cases

Read Article

What Is a Time Series Database? How It Works & Use Cases

Read Article

Top 10 Web Server Security Best Practices

Read Article