Valien
Valien

Reputation: 1145

How to strip the trailing URL in an nginx 404 redirect?

We have an nginx server running and the default 404 is the generic nginx 404. It's now setup to redirect all pages to the index page using this snippet:

error_page 404 =200 /index.html

Seems to be working well but now on the redirected homepage you see the trailing URL instead of the TLD.

Example:

www.somesite.com/whereis404

should redirect to

www.somesite.com

and strip out the

/whereis404

I'm not sure how to strip out that trailing url, etc on the redirect. Any thoughts or is this not doable in nginx?

Upvotes: 0

Views: 207

Answers (1)

Richard Smith
Richard Smith

Reputation: 49702

If you want to change the address bar, you need to perform an external redirect. By specifying a relative URI, nginx performs an internal redirect by default. There are a number of directives which perform an external redirect, but replacing the URI on the error_page directive with a scheme and a host name will instruct nginx to make the redirect external:

error_page 404 =200 $scheme://$server_name/;
error_page 404 =200 $scheme://$host/;

If you are in a reverse-proxy environment, the second variant would be preferable. This assumes you have an index defined, otherwise append index.html.

Upvotes: 1

Related Questions