2

I keep getting ERR_TOO_MANY_REDIRECTS when trying to navigate to the frontend shop of a Prestashop 1.7 site. However it is fine when going to the admin backend.

Any suggestions would be helpful.

Site is running with Nginx, PHP-FPM7, MariaDB.

Nginx config file below:

upstream examplecoukbackend {
        server unix:/var/run/php-fcgi-examplecouk.sock;
}

server {
        listen 80;
        server_name example.co.uk;
        return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;

    ssl_session_timeout 24h;
    ssl_session_cache shared:SSL:10m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:RSA+3DES:AES128-SHA:!ADH:!AECDH:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_certificate /etc/letsencrypt/live/example.co.uk/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.co.uk/privkey.pem;

    server_name example.co.uk;
    root /var/www/vhosts/example.co.uk/htdocs; 
    access_log /var/www/vhosts/example.co.uk/access.log;
    error_log /var/www/vhosts/example.co.uk/error.log;

    index index.php index.html;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        auth_basic off;
        allow all;
        log_not_found off;
        access_log off;
    }

    # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 1;
    gzip_buffers 16 8k;
    gzip_http_version 1.0;
    gzip_types application/json text/css application/javascript;

    rewrite ^/[a-zA-Z][a-zA-Z]/(index\.php.*)$ /$1 last;  #Remove language code when index.php is called directly
    rewrite ^/api/?(.*)$ /webservice/dispatcher.php?url=$1 last;
    rewrite ^/([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+.jpg$ /img/p/$1/$1$2$3.jpg last;
    rewrite ^/([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+.jpg$ /img/p/$1/$2/$1$2$3$4.jpg last;
    rewrite ^/([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+.jpg$ /img/p/$1/$2/$3/$1$2$3$4$5.jpg last;
    rewrite ^/([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+.jpg$ /img/p/$1/$2/$3/$4/$1$2$3$4$5$6.jpg last;
    rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+.jpg$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6$7.jpg last;
    rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7$8.jpg last;
    rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8$9.jpg last;
    rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9$10.jpg last;
    rewrite ^/c/([0-9]+)(-[.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+.jpg$ /img/c/$1$2$3.jpg last;
    rewrite ^/c/([a-zA-Z_-]+)(-[0-9]+)?/.+.jpg$ /img/c/$1$2.jpg last;

    location /admin/ {                           #Change this to your admin folder
        if (!-e $request_filename) {
            rewrite ^/.*$ /admin/index.php last; #Change this to your admin folder
        }
    }

    location / {
        if (!-e $request_filename) {
            rewrite ^/.*$ /index.php last;
        }
    }

    location ~ .php$ {
        fastcgi_split_path_info ^(.+.php)(/.*)$;
        try_files $uri =404;
        fastcgi_keep_conn on;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass examplecoukbackend;  #Change this to your PHP-FPM location
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Log file below, IPs and domain name generalised:

2017/01/08 21:58:00 [notice] 21867#21867: 1 "^/([0-9])(-[_a-zA-Z0-9-])?(-[0-9]+)?/.+.jpg$" does not match "/index.php", client: 0.0.0.0, server: example.co.uk, request: "GET /index.php HTTP/1.1", host: "example.co.uk" 2017/01/08 21:58:00 [notice] 21867#21867: 1 "^/([0-9])([0-9])(-[_a-zA-Z0-9-])?(-[0-9]+)?/.+.jpg$" does not match "/index.php", client: 0.0.0.0, server: example.co.uk, request: "GET /index.php HTTP/1.1", host: "example.co.uk" 2017/01/08 21:58:00 [notice] 21867#21867: 1 "^/([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-])?(-[0-9]+)?/.+.jpg$" does not match "/index.php", client: 0.0.0.0, server: example.co.uk, request: "GET /index.php HTTP/1.1", host: "example.co.uk" 2017/01/08 21:58:00 [notice] 21867#21867: 1 "^/([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-])?(-[0-9]+)?/.+.jpg$" does not match "/index.php", client: 0.0.0.0, server: example.co.uk, request: "GET /index.php HTTP/1.1", host: "example.co.uk" 2017/01/08 21:58:00 [notice] 21867#21867: 1 "^/([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-])?(-[0-9]+)?/.+.jpg$" does not match "/index.php", client: 0.0.0.0, server: example.co.uk, request: "GET /index.php HTTP/1.1", host: "example.co.uk" 2017/01/08 21:58:00 [notice] 21867#21867: 1 "^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-])?(-[0-9]+)?/.+.jpg$" does not match "/index.php", client: 0.0.0.0, server: example.co.uk, request: "GET /index.php HTTP/1.1", host: "example.co.uk" 2017/01/08 21:58:00 [notice] 21867#21867: 1 "^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-])?(-[0-9]+)?/.+.jpg$" does not match "/index.php", client: 0.0.0.0, server: example.co.uk, request: "GET /index.php HTTP/1.1", host: "example.co.uk" 2017/01/08 21:58:00 [notice] 21867#21867: 1 "^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-])?(-[0-9]+)?/.+.jpg$" does not match "/index.php", client: 0.0.0.0, server: example.co.uk, request: "GET /index.php HTTP/1.1", host: "example.co.uk" 2017/01/08 21:58:00 [notice] 21867#21867: 1 "^/c/([0-9]+)(-[._a-zA-Z0-9-]*)(-[0-9]+)?/.+.jpg$" does not match "/index.php", client: 0.0.0.0, server: example.co.uk, request: "GET /index.php HTTP/1.1", host: "example.co.uk" 2017/01/08 21:58:00 [notice] 21867#21867: *1 "^/c/([a-zA-Z_-]+)(-[0-9]+)?/.+.jpg$" does not match "/index.php", client: 0.0.0.0, server: example.co.uk, request: "GET /index.php HTTP/1.1", host: "example.co.uk"

Adam Birds
  • 187
  • 2
  • 3
  • 13
  • Who wrote this configuration? It's quite bizarre. Looks like someone who had no experience with nginx wrote it. Is this what prestashop recommended? – Michael Hampton Jan 08 '17 at 16:42
  • Its off a few different sites, prestashop haven't actually released updated config for 1.7 yet, by the looks of it the rewrite bit was from the 1.6 config. – Adam Birds Jan 08 '17 at 16:54
  • That's really frightening. – Michael Hampton Jan 08 '17 at 16:55
  • From here - https://github.com/MattLoyeD/Prestashop-Nginx/blob/master/prestashop-nginx.conf – Adam Birds Jan 08 '17 at 16:58
  • 1
    That one doesn't look bad. But of course, you need to understand what is going on. Just copying and pasting things randomly without understanding them is just going to cause you pain and frustration. – Michael Hampton Jan 08 '17 at 17:03
  • What are the actual rewrites the web server is sending out? – Tero Kilkanen Jan 08 '17 at 21:30
  • I have enabled rewrite_log and even with the long list of rewrites commented out, it shows too many redirects however none are being logged, when all the long list are enabled it does log a lot of rewrites, i am wondering if this is actually down to the application. I will attach log file for when uncommented. – Adam Birds Jan 08 '17 at 22:02
  • Enabling ssl for all pages worked for me. https://stackoverflow.com/questions/41718937/error-err-too-many-redirect-prestashop-1-7 – Harpreet Singh Apr 25 '20 at 07:37

4 Answers4

2

One problem here is this block:

location / {
    if (!-e $request_filename) {
        rewrite ^/.*$ /index.php last;
    }
}

One should use this instead:

try_files $uri $uri/ /index.php;

And then for your admin:

location /admin/ {                           #Change this to your admin folder
    if (!-e $request_filename) {
        rewrite ^/.*$ /admin/index.php last; #Change this to your admin folder
    }
}

You can use this:

location /admin/ {
    try_files $uri $uri/ /admin/index.php;
}

However, these changes might not solve your issue.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
0

Most probably the Main URL in Prestashop is configured wrong.

From the documentation:

  • Main URL. By enabling this, you indicate that you want all the other URLs for this shop to redirect to this main URL

If you configure http://example.com in Prestashop, but configure nginx to redirect all HTTP requests to HTTPS you will get such a redirect loop.

Make sure you configure the Main URL in Prestashop to the the URL where your httpd redirects to.

Gerald Schneider
  • 23,274
  • 8
  • 57
  • 89
0

if you have this problem only in the front of store then you should go to SET UP URLS->Redirect to the canonical URL and set it to no redirection

-1

I have the same problem when I move my prestashop from local to online. I solved removing. All the content of the .htaccess file. I think the problem arise due conflicts between rewrite rules in a .htaccess and nginx config.

I use this nginx.conf https://github.com/PrestaShop/docs/blob/master/nginx.rst and remove content of .htaccess. The 301 HTTP error just gone.