Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to trim trailing slashes from URLs #3934

Closed
wants to merge 1 commit into from
Closed

Add an option to trim trailing slashes from URLs #3934

wants to merge 1 commit into from

Conversation

jamieconnolly
Copy link
Contributor

@jamieconnolly jamieconnolly commented Oct 3, 2017

It lives behind a trimTrailingSlash option in config.toml. There is no CLI flag for this option.

It works by hooking into the uglyURLs functionality and then stripping out the .html suffix when generating URLs. This means it generates /filename.html files rather than /filename/index.html. It also handles when there's no trailing slash in the page's URL when set through the front matter.

It's all been tested, but if there are any I've missed please let me know.

I've documented the option in my fork of hugoDocs. I'll create a PR if/when this progresses.

See #3556 and https://discuss.gohugo.io/t/links-without-a-trailing-slash/831.

@jamieconnolly jamieconnolly changed the title Add an option to trim trailing slashes from URLs [WIP] Add an option to trim trailing slashes from URLs Oct 3, 2017
@jamieconnolly jamieconnolly changed the title [WIP] Add an option to trim trailing slashes from URLs Add an option to trim trailing slashes from URLs Oct 3, 2017
@jamieconnolly
Copy link
Contributor Author

I forgot to mention that this is now ready to review.

@betaveros
Copy link
Contributor

Any updates on this? I'd love to see this feature in Hugo.

@DonDebonair
Copy link

I'd love to see this merged. I want to migrate my website from Pelican to Hugo, but I want to keep the URLs in tact. And those URLs currently don't have trailing slashes.

@bep
Copy link
Member

bep commented Nov 25, 2017

I will have to think about this one. The change set was a little bit bigger than I imagined. It looks correct, but I'm not sure I'm willing to maintain such a feature.

@DonDebonair
Copy link

DonDebonair commented Nov 25, 2017

With the risk of reopening the whole discussion I saw on Discourse: I understand why the way Hugo currently works is really convenient, because it requires no changes to your webserver. But the reasoning behind the trailing slash just isn't valid 100% of the time.
If you don't need to have a separate directory per blog post, because you have no need for extra files (images, different formats, etc.), you should be able to generate a .html file per blog post, all in 1 directory. And in that case, trailing slashes would be breaking the rules.

This is my current setup in Pelican:

[INPUT]                   [OUTPUT]                [URL]
content/post-1.md -> blog/post-1.html -> https://example.com/blog/post-1
content/post-2.md -> blog/post-2.html -> https://example.com/blog/post-2
...

NGINX is configured in such a way that it will serve blog/post-1.html when blog/post-1 is requested.

This is a valid usecase I'd think, right?

@stale
Copy link

stale bot commented Feb 23, 2018

This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help.
If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open.
If this is a feature request, and you feel that it is still relevant and valuable, please tell us why.
This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

@stale stale bot added the Stale label Feb 23, 2018
@mapx
Copy link

mapx commented Feb 23, 2018

It's a great feature request that worth waiting to be accepted or refused by human.

@stale stale bot removed the Stale label Feb 23, 2018
@stanmots
Copy link

stanmots commented Mar 5, 2018

Too sad this isn't merged. Actually, what Hugo calls "ugly urls" is called "pretty urls" on Netlify. Take a look at their docs:

When “Pretty URLs” is enabled under processing settings for your site, Netlify will enforce consistent URL patterns.

A link to /about.html will be rewritten to /about in your HTML files and Netlify will enforce a redirect from /about/ to /about

It seems urls w/o trailing slashes are a standard in the modern web development. For God's sake, even this very PR url is without a trailing slash.

@kaushalmodi
Copy link
Contributor

@storix

Too sad this isn't merged. Actually, what Hugo calls "ugly urls" is called "pretty urls" on Netlify.

The "prettiness" definition is slightly different, but not the opposite.

Both Hugo and Netlify consider URLs ending in .html as Ugly. That part is consistent.

The only thing is that Hugo doesn't care about removing the trailing slash for non-Ugly URLs, while Netlify removes the trailing slashes too.

FWIW I don't care much about the trailing slashes being present or not.. Why is that a big deal.. just URL aesthetics or a technical reason?

@stanmots
Copy link

stanmots commented Mar 5, 2018

@kaushalmodi

The "prettiness" definition is slightly different, but not the opposite.

Honestly, I find all these config options regarding url prettiness quite confusing. Maybe it's better to rename uglyUrls to something more meaningful. But, okay, this is a different topic.

FWIW I don't care much about the trailing slashes being present or not.. Why is that a big deal.. just URL aesthetics or a technical reason?

It's a big deal when you are migrating a site which was already configured with "slashless" urls. Search engines treat https://example.com/blog/post/ and https://example.com/blog/post as different urls. Currenlty I need to search through the web for some hacks that will force Hugo to remove the trailing slashes.

@stale
Copy link

stale bot commented Jul 3, 2018

This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help.
If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open.
If this is a feature request, and you feel that it is still relevant and valuable, please tell us why.
This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

@stale stale bot added the Stale label Jul 3, 2018
@stale stale bot closed this Aug 2, 2018
@DonDebonair
Copy link

Interesting that this issue is considered "stale", because the maintainers just chose to ignore it. Many compelling arguments have been put forth, but apparently that is not good enough...

@CLAassistant
Copy link

CLAassistant commented Aug 25, 2018

CLA assistant check
All committers have signed the CLA.

@anthonyfok
Copy link
Member

@DannyDev wrote:

Interesting that this issue is considered "stale", because the maintainers just chose to ignore it.

Well, things are not what they appear on the surface. A few observations from an occasional helper:

  • An issue or a PR becomes stale when there is no activity on it for several months, and as you can see, the "stale" tag was added automatically by a bot without no human intervention.
  • While it is a bit sad when an issue or a PR goes stale, it serves as a wake-up call because interested parties including the original submitter will notice the "stale" state, and that is usually enough to spurge everyone into action, if people care about the issue/PR enough.
  • Hugo has gone through tremendous evolution over the past year with lots of exciting new features as well as bug fixes, and I am really amazed by what our Lead Maintainer has accomplished. That said, all maintainers and contributors, including our Lead, are volunteers, and are human. There is a limitation of how much one can accomplish in a day, and there are lots of issues and PRs that still need to be looked after but would have to wait due to lack of manpower. It is like that in almost all open-source projects, so I hope you can understand.
  • It seems the CLA (Contributor License Agreement) isn't signed? Was that the case even before this PR went stale? If that is the case, that would be a major obstacle which prevents the PR from being accepted.
  • For better or for worse, 10 months have passed since the initial PR, and due to Hugo's major new features and refactoring, there are now merge conflicts that need to be resolved.

betaveros added a commit to betaveros/hugo that referenced this pull request Oct 10, 2018
See gohugoio#3934 . I did not rebase some
of the added tests because their merge conflicts were more complicated;
this is a proof of concept, and there are apparently licensing issues
that need to be resolved.
@betaveros
Copy link
Contributor

As you can see from the GitHub notification, I tried rebasing this PR onto master. I didn't get to rebasing all the tests yet, but the feature seems to work overall if I manually test against my own static site and, if it would be enough to get this merged, I can try to rebase the tests or write equivalent ones in a few days. (Are there licensing issues? The original author has made a PR before #3933 and the CLA bot reports that they signed the CLA there; I also think I signed the CLA when submitting previous PRs, but the bot no longer seems to think that I have.) I still care a good deal about this feature and consider it essential for me to use Hugo, FWIW.

(Incidentally, Hugo is orders of magnitude faster for me since the rebase — kudos to the maintainers' effort!)

The diff so far is here: master...betaveros:trim-trailing-slash

@ngocketit
Copy link

ngocketit commented May 2, 2019

Any update on this please? Having URLs with a slash is a stopper for me to migrate a site to Hugo now

@hanzei
Copy link

hanzei commented May 3, 2019

@ngocketit Could you please elaborate why this is a blocker for you?

@ngocketit
Copy link

@hanzei I intend to host the site on S3 with CloudFront and HTTPS. With the trailing slash, CloudFront fails to serve the site as it couldn't route, for example, https://www.blabla.com/about/ to https://www.blabla.com/about/index.html and I don't want to use ugly URLs.

@jamieconnolly
Copy link
Contributor Author

CLA is signed 👍

@piotrradzewicz
Copy link

Hey guys! Could you resolve conflicts and merge this PR? =) I'd really appreciate it

@DonDebonair
Copy link

Calling @bep to merge this

nanoant pushed a commit to nanoant/hugo that referenced this pull request Nov 9, 2019
See gohugoio#3934 . I did not rebase some
of the added tests because their merge conflicts were more complicated;
this is a proof of concept, and there are apparently licensing issues
that need to be resolved.
@johnsoncodehk
Copy link

johnsoncodehk commented Nov 16, 2019

Since it has not been merged, this is a quick fix. (only change the look, not really change the url)

<!-- Put in <header> or anywhere -->
<script>window.history.replaceState("", "", window.location.href.replace(new RegExp("/(?!.*/)"), ""));</script>

@maximelouet
Copy link

I personally use a custom regex-based script in order to remove the trailing slashes. Actually, I enable the ugly URLs option which appends .html to all pages instead of slashes. Then I replace all occurences of .html while carefully making sure I only target my own website's links and not the external ones. I execute this script against the generated public directory after running hugo.

It works quite well but it took me a while to make it work properly. It's kinda sad such a basic feature is not implemented in Hugo; a blog post should not end with a slash as it is not a directory...

@christian-geier
Copy link

Hi! Just curious what the status is on this ? Simply cannot believe that this is not an option. Hope this will be implemented soon...

@RedFolder
Copy link

RedFolder commented Jan 27, 2020

It is absolutely ridiculous that this is not yet an option. As rosell pointed out 2+ years ago, plenty of CMS'es offer this. https://discourse.gohugo.io/t/hugo-support-for-urls-without-a-trailing-slash/6763/3
All code is opinionated, but this is too much.

@jamieconnolly , I am happy to assist with any merge conflicts if you'd like to add me to the repo :)

@virgofx
Copy link
Contributor

virgofx commented Feb 7, 2020

@bep Anyway we can get a status update on this? This is very much needed!

@bep
Copy link
Member

bep commented Feb 7, 2020

@bep Anyway we can get a status update on this? This is very much needed!

I'm closing this for now. This is too big of a change for me to review/merge/maintain (I don't need this myself). We may revisit this later if/when we get more long term maintainers. This is open source: Time is limited.

@bep bep closed this Feb 7, 2020
@RedFolder
Copy link

In that case, I guess I will be moving on from Hugo.

@dcu
Copy link

dcu commented Feb 9, 2020

when hosting on Cloudfront + S3, it doesn't use index.html for subdirectories so a common hack is copying foo/index.html to foo to simulate it... but it doesn't work with trailing slashes
is there an alternative? (different than using lambda edge which is hack as well)

@jloh
Copy link
Contributor

jloh commented Feb 9, 2020

@dcu yes, point CF at your S3 static website URL and not directly at the bucket. https://www.reddit.com/r/aws/comments/68on7h/indexhtml_in_subfolders_via_cloudfront/

betaveros added a commit to betaveros/hugo that referenced this pull request Feb 10, 2020
See gohugoio#3934 . I did not rebase some
of the added tests because their merge conflicts were more complicated;
this is a proof of concept, and there are apparently licensing issues
that need to be resolved.
@betaveros
Copy link
Contributor

Sad to hear but understandable. FWIW I will probably be "maintaining" this feature by rebasing it onto my fork for as long as it's not too hard (no conflicts since when I last did this 8 months ago), with the caveat that there are basically no code tests left (doesn't seem worth it if it won't be merged; my standard for it working is "nothing breaks when I click around my blog").

@virgofx
Copy link
Contributor

virgofx commented Feb 10, 2020

Just to clarify -- it does seem like 95% of the work is already done. @bep if the PR is updated with new tests would you consider merging this? (would you prefer a new PR?) It just basically allows an option as a flag. The code changes are not that extensive ... pretty straightforward and could easily be managed moving forward from maintainers plus the community.

@sdwolfz
Copy link

sdwolfz commented Mar 24, 2020

Just wanted to let you know that I'm also interested in this functionality, but I don't have the knowledge to take over and improve this PR 😞 .

serebrov added a commit to serebrov/serebrov.github.io that referenced this pull request Apr 11, 2020
Hugo adds trailing slashes by default and I didn't find a way to remove it in some other way.
There is [permalinks] setting in config, which I changed to `posts = "/html/:slug.html"`,
but there is still a traling slash.

There is also an unmerged PR: gohugoio/hugo#3934
Sidenote: maintainer said he won't merge it because he doesn't need this feature and doesn't
want to spend time on review / merge / maintainance.
@crazyoptimist
Copy link

+1 here
I also want this func!

@sdwolfz
Copy link

sdwolfz commented Sep 25, 2020

So I tried adding support for this in GitLab pages a long time ago but I did not have time to work in it further, see: https://gitlab.com/gitlab-org/gitlab-pages/-/merge_requests/259 my plan was to start working on this after the former MR was merged. I might look into this in the future but my hands are tied these days. Still want this functionality though.

betaveros added a commit to betaveros/hugo that referenced this pull request Oct 3, 2020
See gohugoio#3934 . I did not rebase some
of the added tests because their merge conflicts were more complicated;
this is a proof of concept, and there are apparently licensing issues
that need to be resolved.
@Hans5958
Copy link

Hans5958 commented Nov 25, 2020

+1, if Jekyll does it, GitHub Pages does it, and Netlify does it, why not Hugo?

Such a shame that this PR get closed. At least GitHub Pages won't do the trailing slash after publishing, that is. (maybe, please don't @ me)

@joaopgrassi
Copy link

@Hans5958 curious on how you said GitHub pages won't do the trailing slash? Just hosted there now and I have them.. curious on what you did?

@Hans5958
Copy link

Hans5958 commented Dec 5, 2020

@joaopgrassi That is just a theory. I didn't test it so don't take my word from it.

@joaopgrassi
Copy link

@Hans5958 ah ok.. well I can tell you that it does have the trailing slash on my blog hosted on gh-pages. So there's that :)

@BDHU
Copy link

BDHU commented Apr 16, 2021

Great. I was trying to move everything from Jekyll to Hugo and noticed there's this trailing slash I can't get rid of. Now the hell is let loose and it just added extra work for me to update all url. The reason I picked Hugo in the first place was so that my life would get easier, not harder...

@gohugoio gohugoio locked as too heated and limited conversation to collaborators Apr 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.