NGINX redirect 404 errors to an external URL

11,734

Inside your server http { ... } block in nginx.conf you can add:

error_page 404 /example.com.404.html;

location = /example.com.404.html {
  root    html;
  allow   all;
  index   example.com.404.html
  rewrite ^ $scheme://www.someothersite.com$request_uri permanent;
}
Share:
11,734

Related videos on Youtube

Jordan Bendon
Author by

Jordan Bendon

Updated on September 18, 2022

Comments

  • Jordan Bendon
    Jordan Bendon over 1 year

    I haven't found any way to configure 404 errors to redirect to an external URL like http://google.com.

    I have found ways to redirect to another page like /404.html, but not an external URL.

    For instance, I would like to redirect any 403 or 404 errors to http://google.com. Is this possible?

    Thank you for help.

  • Jordan Bendon
    Jordan Bendon over 10 years
    i have added this block directly in my server block but apparently there is no redirection, can you explain me how it works plz, this may work ? error_page 404 /40x.html; location = /40x.html { root html; rewrite ^(.*) google.com permanent; }
  • Richard
    Richard over 10 years
    You should include your domain on the error page's path, so you now have a location block for your website, then a separate location for each 40x error type you wish to redirect.
  • Richard
    Richard over 10 years
    The actual path should be different. I could have /home/richard/public_html as root for my website and /home/richard/public_html/errors for 40x's. Specify the error page html as the index page to redirect from within the new location block.