Is there a feature to skip the trailing slash in the URL?

I prefer my URL to look like baseURL/blog over baseURL/blog/
similarly baseURL/blog/post-name over baseURL/blog/post-name/

I saw many people are having same requirements in this closed discussion.

Is there an existing feature to do this in a pretty way? Else can’t we have this feature as it looks better than the existing one?

Is there an existing feature to do this in a pretty way? Else can’t we have this feature as it looks better than the existing one?

It does not look better than the existing one :wink: that’s just your preference…

It’s not possible currently with Hugo features. Also, you want to “cover-up” your file structure. A slash at the end is basically what “god” (tim berner lee) intended to be. It means that the file you are loading is in the folder https://mydomain.com/apath/. without that slash, you would want the file apath in the root folder of your website. Is it there, or is It in /apath/index.html?

Long story short: you are on your own, if you want to create weird URLs. It’s probably possible with an .htaccess file that in the end shows an URL while loading a proper path.

People like to forget that an URL is a path to a file and only “index.ext” is the part that can be left out of a link. It’s in a folder. Which is “behind” a slash.

1 Like

Hi, I get your point. But correct me if I’m wrong, isn’t that only applicable to baseURL/blog/ in this case? Considering post-name.md is a file inside /blog/, this should be baseURL/blog/post-name right?

That has to be possible. Normal people just don’t type in a url with a slash at the end. That’s confined to a small sliver of tech folk. Especially when you tell someone a URL.

1 Like

It’s a static site, meaning it’s just normal HTML files in normal directories. The reason why it’s /post-name/ is because the browser automatically tries /post-name/index.html, which is what Hugo generates.

Hugo has no control over how browsers present directories and index.html files. If you want to do that, you would need to configure the web server, e.g. configure it to trim trailing slashes, and resolve URLs without trailing slashes to the corresponding directories.

3 Likes

Right. That was that. Has to be done on the server level. Just recalled I have the trailing slash stuff working in AWS but it was a pain to set up.

1 Like

I think that that’s a misconception. It is the server who “typically” delivers the file index.html if the browser asks for a directory:

https://httpd.apache.org/docs/trunk/getting-started.html#content

“index.html” is only a convention, and you could configure your server to deliver basically anything if it receives a directory URL.

Omitting the trailing slash at the end of an URL leads to a server redirect to the URL with the slash appended. Which is not good performance-wise.

4 Likes

Check the output of your hugo command. While the URL might be baseURL/blog/post-name internally, it’s in a folder public/blog/post-name/index.html - and that is why any browser adds a slash at the end. I am pretty sure that your URL if you finally get rid of that slash, will have no slash, but for the browser, it will still be a slashed URL in the end. Some providers like Netlify even add the slash automatically to your URL.

1 Like

No, they do not. It is the server that sends a redirect request if the trailing slash is missing from an URL pointing to a directory, like described here for Apache:

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

4 Likes

You are right, well, I phrased it wrong, long day :wink: What I mean is that if inside a folder bla is an index.html then you will end up with a slash after bla - done by magic (yes, the server). But thanks for the correction :slight_smile: better to be 100% right.

1 Like

Omitting the trailing slash at the end of an URL leads to a server redirect to the URL with the slash appended. Which is not good performance-wise.

@chrillek Thanks for pointing that out. The way I thought of it had been built only on intuition, but your explanation makes more sense.

1 Like

You can enable ugly urls if you don’t want a trailing slash in the content Hugo generates.

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.