Remove trailing slash from - 2023 edition

Hello,

I saw there were several in the past discussions about configuring URLs without trailing slash.

Is this still not possible in Hugo?

I understand that URL’s without trailing slash is rather a matter of personal preference. I think no trailing slash makes URL simply look better. I don’t see why URL’s must mimic layout on disk. In Jekyll I can configure URL’s without trailing slash:

permalink: /:title

For Jekyll site, in nginx conf, here’s how I configure non-trailing slash and redirect requests to *.html to non-html URL:

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

Is there a way to have a syndicated feature sponsorship? I’d pay some money for this feature.

Not necessarily. If the URL refers to a directory, the missing slash induces a redirect, thus a second HTTP roundtrip.

Here’s more detail on that:

I never thought of a beauty contest featuring URLs.

There’s no “must” here. It is simply the web server that is working with what it finds on disk. You tell it to look for a file (no trailing slash). The file is not there, but a directory is. So the server sends a redirect with the trailing slash.

https://httpd.apache.org/docs/2.4/mod/mod_dir.html

(I don’t know how nginx and other servers handle this, though). On Apache, you can perhaps rewrite the URL without a redirect, but that requires mod_proxy to be installed and seems to entail other performance penalties.

1 Like

I can configure the web server to server both /file.html as URL /file and /directory/ as URL /directory.

The question is if I can configure Hugo to generate URL’s like /file and /directory (in variables, sitemap etc.).

If not, is there an option for a syndicated sponsored feature?

(URL’s really are a beauty contest for me)

No, you cannot.

You could certainly use something like this in your templates…

{{ .Permalink | strings.TrimSuffix "/" }}

…but in my view this is not a great idea.

2 Likes

Indeed, not the best option to add filter on every URL.

Any chance to get this as sponsored feature?

I think it is unlikely:

  1. The current behavior is technically correct (i.e., request directory, only one roundtrip).
  2. Although we could make it configurable for those who can and want to implement server-side URL rewrites, with near zero demand the cost far outweighs the benefit.

I do not see the real issue… but maybe this can help you?

How about allowing no URL on simple files (not directories).

A directory URL can be /dir-name/, but for simple file, I’d like to be able to configure no file extension: /file-name. I just need Hugo to generate file on disk with no extension and use the same file with no extension when generating the URL variables.

Is this possible?

(I’d make the necessary settings in the web server config).

No, it is not.