How to solve ‘too many redirects’ issue in Magento 2?
First of all, if one page in your Magento 2 store gets err_too_many_redirects, you need to clean the cache and cookies. Also, you can check if this page has an error in other browsers.
If the error hasn’t gone, you need to check redirects in Developer Tools. Press the F12 key, navigate to the Network tab, and refresh the problem page. After that, you will see the full list of redirects that it makes. So you can analyze if redirects are between a few different things or it’s redirecting to the same thing.
Check redirects with cURL
You can test your website with the curl. This script can be used on any Unix-like system.
Copy this code into a text editor and save it as redirects.sh.
#!/bin/bash
echo
for domain in $@; do
echo --------------------
echo $domain
echo --------------------
curl -sILk $domain | egrep 'HTTP|Loc' | sed 's/Loc/ -> Loc/g'
echo
done
Then set this file as executable:
chmod +x redirects.sh
Now you can run the script. Just add the needed domain after the script name.
Here is an example of what you get, if the redirect is working fine:
./redirects.sh abc.com
--------------------
abc.com
--------------------
HTTP/1.1 301 Moved Permanently
-> Location: https://abc.com/
HTTP/1.1 301 Moved Permanently
-> Location: https://www.abc.com/
HTTP/1.1 200 OK
The endless redirects from HTTP to HTTPS look like this:
./redirects.sh http://www.abc.com
--------------------
http://www.abc.com
--------------------
HTTP/1.1 301 Moved Permanently
-> Location: https://www.abc.com/
HTTP/1.1 301 Moved Permanently
-> Location: http://www.abc.com/
HTTP/1.1 301 Moved Permanently
-> Location: https://www.abc.com/
HTTP/1.1 301 Moved Permanently
-> Location: http://www.abc.com/
HTTP/1.1 301 Moved Permanently
....
Redirects in the .htaccess file
Below are 3 examples of redirects that you can find in your .htaccess file:
1) Force HTTPS
This code analyzes if the request uses HTTPS. If no, then the setting will redirect to the HTTPS version of the website.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
2) Force non-www
This redirect checks if the website was requested with www and redirects to the non-www domain name.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC] RewriteRule (.*)
http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
3) Force www
This redirect is the opposite of the previous one. It also checks if the website was requested with www but redirects to the www domain version.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule (.*)
http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Redirect configurations in Magento 2
To solve the too many redirects issue in Magento 2, the admin needs to configure local.xml or env.php.
Also, you can set up a prefix for the table names of the Magento 2 database. The main table of the database usually is named core_config_data. In this table, you can configure many potential URLs but they all have base_url. You can run the following command:
mysql -e 'select * from core_config_data where path rlike "base_url"' magento_database
+-----------+---------+----------+-----------------------+----------------------------+
| config_id | scope | scope_id | path | value |
+-----------+---------+----------+-----------------------+----------------------------+
| 3 | default | 0 | web/unsecure/base_url | http://www.abc.com |
| 4 | default | 0 | web/secure/base_url | http://www.abc.com |
+-----------+---------+----------+-----------------------+----------------------------+
To update the base_urls in the Magento 2 database, run:
mysql -e 'update core_config_data set value="https://www.abc.com" where path rlike "web/.*/base_url"' magento_database
To configure smart redirects avoiding broken links and improving indexing, use our SEO Toolkit. The module includes a whole set of tools that will help you cope with the task without special skills.
Login and Registration Form