Removing HTML extension with Nginx in NixOS
The Problem
Having your website end in ".html" doesn't look cool at all B)
Example
https://ammar.engineer/about.html
https://ammar.engineer/about
The first one looks ugly, doesn't it?
How my config looks like:
(Don't forget to open ports!)
security.acme = {
acceptTerms = true;
# email = "email@domain.com"; # I decide to place my email on each domain explicitly
};
services.nginx.enable = true;
services.nginx.recommendedOptimisation = true;
services.nginx.recommendedTlsSettings = true;
services.nginx.recommendedProxySettings = true;
services.nginx.recommendedGzipSettings = true;
security.acme.certs."ammar.engineer".email = "email@domain.com";
services.nginx.virtualHosts."ammar.engineer_main" = {
serverName = "ammar.engineer";
forceSSL = true;
enableACME = true;
locations."/" = {root = "/var/www/ammar.engineer";};
};
Fixing it
# [ .. Truncated ..]
services.nginx.virtualHosts."ammar.engineer_main" = {
serverName = "ammar.engineer";
forceSSL = true;
enableACME = true;
locations."/" = {
root = "/var/www/ammar.engineer";
# <<<<<<<<<<<<<
# forward /somthing.html to /something
# https://stackoverflow.com/questions/38228393/nginx-remove-html-extension
extraConfig = ''
if ($request_uri ~ ^/(.*)\.html) {
return 302 /$1;
}
try_files $uri $uri.html $uri/ =404;
'';
# >>>>>>>>>>>>>
};
};
That's it!