سؤال

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