Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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;
}

}

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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/;
    }
}
share|improve this answer
    
Thanks! I didn't know the return directive! –  gere Nov 3 '12 at 13:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.