What I want to do whenever I get a 404 error on my domain, automatically 301 to the homepage.
I have a lot of old blog posts and such that were linked to, but I don't have them on the blog and if anyone happens to click through from another site that they get kicked to the homepage.
How can I do this inside nginx?
server {
listen 12680;
root /home/noahc/webapps/nginx/html/noahc/;
server_name www.noahc.net, noahc.net;
error_page 404 @foobar;
location @foobar {
rewrite .* / permanent;
}
}
Temporary and Permanent Nginx Redirect Explained On the other hand, a permanent Nginx redirect informs the web browser that it should permanently link the old page or domain to a new location or domain. To map this change, the redirects response code 301 is used for designating the permanent movement of a page.
Essentially, the “404 error” indicates that your or your visitor's web browser was connected successfully to the website server or the host. However, it was unable to locate the requested resource, such as filename or any specific URL.
The error_page
setting can do this for you already:
error_page 404 =301 http://example.com/;
There's a faster way through it:
error_page 404 = @foobar;
location @foobar {
return 301 /;
}
By "faster" I mean "no useless regexp matching inside web server" by using return instead of rewrite.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With