1

I have a static site and I'm trying to remove the .html from the url, so when I visit the site www.example.com/home.html I should get: www.example.com/home.

This is my project structure:

project
    docker
       nginx
           conf.d
               default.conf
    www
        index.html
        home.html
        services.html
docker-compose.yml

This is the docker-compose.yml file:

version: '3.6'

services:
  my-app:
    container_name: trasauto_nginx
    image: nginx:stable-alpine
    restart: always
    volumes:
      - "./www/:/usr/share/nginx/html"
      - ./docker/nginx/conf.d/:/etc/nginx/conf.d
    expose:
      - 80

networks:
  default:
    external:
      name: proxy

and this is the default.conf:

server {
    try_files $uri.html $uri $uri/ =404;
}

I tried to remove the .html part from the url, but for some reason I get:

404 not found

what I did wrong?

1 Answer 1

1

You need to add some more lines to your Nginx configuration in order to have what you want. I think this question has already been answered in detail here.

Edited. Here's an example Nginx configuration that should work

server {
    listen 80;    
    index index.html;

    root /usr/share/nginx/html;

    location / {
        if ($request_uri ~ ^/(.*)\.html) {
            return 302 /$1;
        }
        try_files $uri $uri.html $uri/ =404;
    }
}

You need to specify the root directory from which you're serving your static files.

Sign up to request clarification or add additional context in comments.

3 Comments

I already tried that .. I added the location directive within server but I still get 404
Well, you probably need a bit more lines in your configuration. I didn't realize you only had this one. I'll edit the answer and include the example configuration.
You're right! That's why didn't worked before, now is working fine, many thanks and have a good day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.