1

I have a simple static web site on Nginx, and I'm trying to implement a redirect from mysite.com/index.html to mysite.com, so it's more search engine friendly. But I get a redirect loop. I wrote this rule:

location =  /index.html {
    rewrite ^ http://mysite.com  permanent;
}

Doing some tests, I noticed that if I redirect to the 404 page, everything is fine:

rewrite ^ http://mysite.com/404.html  permanent;

This is the full configuration file:

server {
    listen 80;
    server_name www.mysite.com;
    rewrite ^/(.*) http://mysite.com/$1 permanent;
}

server {
    listen 80;# default_server;
    listen ipaddress:80;
    server_name mysite.com;
    access_log  /var/www/mysite.com/logs/access.log;
    error_log /var/www/mysite.com/logs/error.log;

    root /var/www/mysite.com/htdocs;
    error_page 404 /404.html;
    location =  /index.html {
    rewrite ^ http://mysite.com  permanent;
}

}

1 Answer 1

1

You're doing it wrong.

server {
    listen 80;
    server_name www.mysite.com;
    return 301 http://mysite.com$request_uri;
}

server {
    listen 80 default_server;
    server_name mysite.com;

    access_log  /var/www/mysite.com/logs/access.log;
    error_log /var/www/mysite.com/logs/error.log;

    root /var/www/mysite.com/htdocs;
    error_page 404 /404.html;

    location / {
        try_files $uri $uri/index.html =404;
    }

    location = /index.html {
        return 301 http://mysite.com/;
    }
}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .