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.


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;
| improve this answer | |
  • 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; } – WhatsUp Jan 27 '14 at 9:29
  • 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 Jan 29 '14 at 14:48
  • 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. – Richard Jan 29 '14 at 14:50

Add redirect javascript code to your 404.html page so that whenever your error page loads it redirects to the mentioned target website.

<script type='text/javascript'>
top.location.href = 'http://www.your_target_site.com';

| improve this answer | |
  • I have a meta redirection in 404.html but it s very slow to do redirection, i think there is a mean to do this redirection in the nginx.conf – WhatsUp Jan 24 '14 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.