Adam Bardon
Adam Bardon

Reputation: 3889

nginx location rewrite anything after / to root

Let's say I have example.com and I'd like to rewrite anything after / to root, i.e. example.com/one/two?whatever=foo should go back to example.com

I've tried the following:

location = / {
    index  index.html;
}

location / {
    rewrite ^ / permanent;
}

But this gives me too many redirects error. I could go route using regex to specify all the allowed/disallowed characters, but that would make it too ugly/long/complex.

Why does the exact match can't tell the difference?

Upvotes: 2

Views: 264

Answers (1)

Richard Smith
Richard Smith

Reputation: 49682

You use the index directive to perform an internal rewrite from / to /index.html. See this document for details.

nginx then restarts the search for a matching location, which results in a loop.

You could add an exact match for the /index.html URI, for example:

location = /index.html { }

If index.html pulls in local resources (e.g. css, js, images), you will also need to handle those URIs specially.

Upvotes: 1

Related Questions