[solved] Nginx 301 /path/index.html -> /path/ keeps giving me a 301 loop

blunix

Active Member
Joined
Mar 10, 2024
Messages
197
Reaction score
116
Credits
1,785
I can't seem to figure this one out. I have a website thats just html + css + a tiny bit of js. I am trying to eleminate double pages for SEO - that means that example.com/de/ and example.com/de/index.html show the same page. Google doesn't like that, so example.com/de/index.html should 301 to example.com/de/.

This already works for example.com/index.html 301 to example.com/. But for a path I just can't figure out how to do that. "The internet" advises to use funky if statements and regex but if is evil with nginx so I'm wondering if I'm just overlooking something very simple here x)

Here is my nginx vhost:

Code:
server {

    listen 443 ssl http2;
    server_name www.example.com;
    [ssl stuff here]

    root /var/www/www.example.com/html;

    # Handles requests to https://www.example.com (no trailing slash)
    location = / {
        try_files /index.html =404;
    }

    # Return 301 to / for direct requests to /index.html (SEO no double pages) <== THIS ONE WORKS
    location = /index.html {
        return 301 https://www.example.com/;
    }

    # Return 301 for direct requests to /de/index.html  <== THIS ONE DOESN'T
    location = /de/index.html {
        return 301 https://www.example.com/de/;
    }

If I enable the last location block it gives me a 301 loop:

Code:
r@host:~$ curl -I -L https://www.example.com/de/
HTTP/2 301
server: nginx
date: Sat, 23 Mar 2024 22:24:58 GMT
content-type: text/html
content-length: 3853
location: https://www.example.com/de/
etag: "65fee320-f0d"
strict-transport-security: max-age=63072000

HTTP/2 301
server: nginx
date: Sat, 23 Mar 2024 22:24:58 GMT
content-type: text/html
content-length: 3853
location: https://www.example.com/de/
etag: "65fee320-f0d"
strict-transport-security: max-age=63072000

and so on...
 
Last edited:



Members online


Top