V
V
Viktor2015-10-09 19:48:28
Nginx
Viktor, 2015-10-09 19:48:28

Nginx how to make normal redirect from .html to address without extension?

I recently switched to Ngnix using this docs.rtcamp.com/easyengine/.
Everything pleases, wordpress is installed and works quickly.
However, there are problems with the usual static html site.
Its structure is as follows:
Directory
Directory
index.html
page1.html
page2.html
page3.html
If on Apache (previously configured redirects from www in .htaccess, and .html became generally wonderful), everything works correctly - when you go to site.ru/ page1 opens a page on the page1.html server and stuff like that,
then only the main site opens on Ngnix, site.ru(page index.html), at the addresses site.ru/page1 (or site.ru/page1/ - with a slash) the error "404 Not Found" pops up. Pages open only directly with extensions, for example site.ru/page1.html .
Question - how and how to set up redirects in the Ngnix web server in my case, so that everything works correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Levin, 2015-10-19
@ilyachase

Try it:

location / {
  # do nothing it the extension is already present
  if ($request_filename ~* ^.+.html$) {
    break;
  }
  # add .html if it was not present
  if (-e $request_filename.html) {
    rewrite ^/(.*)$ /$1.html permanent;
    break;
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question