我有一个简单的Nginx的静态网站,我试图实现从mysite.com/index.html到mysite.com的redirect,所以它更适合search引擎。 但是我得到一个redirect循环。 我写了这个规则:
location = /index.html { rewrite ^ http://mysite.com permanent; }
做一些testing,我注意到,如果我redirect到404页面,一切都很好:
rewrite ^ http://mysite.com/404.html permanent;
这是完整的configuration文件:
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; }
}
你这样做是错的。
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/; } }