O
O
Oleg Ulyanov2016-04-10 10:30:33
Nginx
Oleg Ulyanov, 2016-04-10 10:30:33

How to set up a redirect in nginx taking into account the existence of a file?

Good day!
There is a website sitename.ru There is
such a structure of folders/files relative to $ root :
./test.html
./test2
./test2/index.html
./test2/b.html ./test.html (if there is none, check the next option) =>./test/index.html (if there is, we return it, otherwise...) => 404 sitename.ru/test2 => ./test2.html (if there is no such thing, check the next option) =>./test2/index.html (if there is, we return it, otherwise...) => 404 sitename.ru/test2/b => ./test2/b.html (if there is no such, check the next option) => ./test2/b/index.html (if there is, we give it away, otherwise...) => 404



What is the correct way to write rewrite for such conditions, so that it is unified, without being tied to the name of the directory (there are many files and directories)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2016-04-10
@gelosoft

It is correct to write try_files

location / {
    try_files $uri $uri.html $uri/index.html =404;
}

And if I type example.com/test/(with a slash at the end), then what should come out?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question