0

So I have been trying to get my site working in a way where:

example.com shows example.com/index.html
example.com/test shows example.com/test.html
and so on.

So my current config file is this:

server {
    listen      80;
    listen      [::]:80;
    server_name example.com;
    root        /var/www/html;
    index index.html index.php;

    location / { 
        try_files $uri.html $uri $uri/ @htmlext;
    }   

    location ~ \.html$ {
        try_files $uri =404;
    }

    location @htmlext {
        rewrite ^(.*)$ $1.html last;
    } 

    error_page 404 /404.html;
}

So I was Googling all yesterday and couldn’t get it to work. So thanks in advance!

2
server {
  listen      80;
  listen      [::]:80;
  server_name example.com;
  root        /var/www/html;
  index index.html index.php;

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

  error_page 404 /404.html;
}
0

Below config gives you the following:

curl http://example.com/test --> output: content of test.html

curl http://example.com/ --> output: content of index.html

server {
    listen      80;
    listen      [::]:80;
    server_name example.com;
    root        /var/www/html;
    index index.html index.php;

    location /test {
        try_files $uri.html $uri $uri/;
    }
   
    location / { 
        try_files $uri.html $uri $uri/;
    }   

    location ~ \.html$ {
        try_files $uri =404;
    }

  

    error_page 404 /404.html;
}
4
  • 1
    What is the meaning of "@htmltext;" ? – Gerard H. Pille Aug 20 '20 at 15:13
  • There is no point to have separate @htmlext location, because the try_files already includes .html suffix. There isn't a need for duplicate location /test block either. – Tero Kilkanen Aug 20 '20 at 17:08
  • Yeah of course not. But i wanted to minimize my configuration changes. Next time i will cut off this block and explain it in the answer. – Lorem ipsum Aug 20 '20 at 17:12
  • The answer still has @htmlext in the try_files. That is not needed. – Tero Kilkanen Aug 23 '20 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.