문제

I'm shutting down a site and I need to 301 redirect all pages to the home page where I have a message saying that the site is being closed down.

Basically any http://example.com/anyfolder -> 301 to http://www.example.com

I have the following but this results in a redirection loop.

location ~ ^/([A-z]+) { rewrite ^ / permanent; }

What is the proper way to do this in nginx?

도움이 되었습니까?

해결책

This worked for me. This is assuming you only have a index.html,htm and the other urls are missing the physical file on disk.

server {
  listen      80;
  server_name www.example.com example.com;
  root /u/apps/example/www;
  index index.html;

  location / {
    if (!-e $request_filename) {
      rewrite ^ / permanent;
    }
  }
}

다른 팁

Make it simple.

location / {
    rewrite ^ http://example.com;
}

location = / {
}

The below is a more modern syntax for trying various file resources for existence, with progressive defaults.

Be sure to uncomment other location paths underneath the one below, to avoid more specific matches, if necessary.

Also, if is evil when used in location context.

server {
    listen 1.2.3.4:80;
    server_name example.com;
    
    root /path/to/document/root;

    index index.html;

    location / {
        try_files $uri $uri/ /index.html;
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top