title: How do I force redirect all 404's (or every page, whether invalid or not) to the homepage?
body: <p>Currently every invalid page is 500 (Internal Server Error) because I probably messed up with my server block configuration.</p>
  
  <p>I decided to shut down my website a while ago and created a simple one-page, thank-you homepage. However old links and external sites are still trying to access other parts of the site, which no longer exists.</p>
  
  <p>How do I force redirect all non-homepage (any invalid URL) to the homepage?</p>
  
  <p>I tried with the following block, but it didn't work:</p>
  
  <pre><code>location / {
      try_files $uri $uri/ $document_uri/index.html;
  }
  </code></pre>
  
  <p>My current configuration is (I don't even serve PHP files right now, ie homepage is simple html):</p>
  
  <pre><code>server {
      server_name www.example.com example.com;
      access_log /srv/www/example.com/logs/access.log;
      error_log /srv/www/example.com/logs/error.log;
      root /srv/www/example.com/public_html;
      index index.php index.html;
  
      location / {
          try_files $uri $uri/ $document_uri/index.html;
      }
  
      # Disable favicon.ico logging
      location = /favicon.ico {
          log_not_found off;
          access_log off;
      }
  
      # Allow robots and disable logging
      location = /robots.txt {
          allow all;
          log_not_found off;
          access_log off;
      }
  
      # Enable permalink structures
      if (!-e $request_filename) {
          rewrite . /index.php last;
      }
  
      # Handle php requests
      location ~ \.php$ {
          try_files $uri = 404;
          include /etc/nginx/fastcgi_params;
          fastcgi_pass  127.0.0.1:9000;
          fastcgi_index index.php;
          fastcgi_send_timeout 900;
          fastcgi_read_timeout 900;
          fastcgi_connect_timeout 900;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      }
  
      # Disable static content logging and set cache time to max
      location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
          access_log off;
          log_not_found off;
          expires max;
      }
  
      # Deny access to htaccess and htpasswd files
      location ~ /\.ht {
          deny  all;
      }
  
      # Deny access to hidden files (beginning with a period)
      location ~ /\. {
          access_log off; log_not_found off; deny all;
      }
  }
  </code></pre>