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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions commands/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,15 @@ func (c *commandeer) serve(port int) {
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
w.Header().Set("Pragma", "no-cache")
}

if c.Cfg.GetBool("trimTrailingSlash") {
path := strings.Split(r.URL.Path, "/")

if !strings.Contains(path[len(path)-1], ".") && !strings.HasSuffix(r.URL.Path, "/") {
r.URL.Path += ".html"
}
}

h.ServeHTTP(w, r)
})
}
Expand Down
2 changes: 2 additions & 0 deletions helpers/pathspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type PathSpec struct {

disablePathToLower bool
removePathAccents bool
trimTrailingSlash bool
uglyURLs bool
canonifyURLs bool

Expand Down Expand Up @@ -77,6 +78,7 @@ func NewPathSpec(fs *hugofs.Fs, cfg config.Provider) (*PathSpec, error) {
Cfg: cfg,
disablePathToLower: cfg.GetBool("disablePathToLower"),
removePathAccents: cfg.GetBool("removePathAccents"),
trimTrailingSlash: cfg.GetBool("trimTrailingSlash"),
uglyURLs: cfg.GetBool("uglyURLs"),
canonifyURLs: cfg.GetBool("canonifyURLs"),
multilingual: cfg.GetBool("multilingual"),
Expand Down
2 changes: 2 additions & 0 deletions helpers/pathspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestNewPathSpecFromConfig(t *testing.T) {
l := NewLanguage("no", v)
v.Set("disablePathToLower", true)
v.Set("removePathAccents", true)
v.Set("trimTrailingSlash", true)
v.Set("uglyURLs", true)
v.Set("multilingual", true)
v.Set("defaultContentLanguageInSubdir", true)
Expand All @@ -48,6 +49,7 @@ func TestNewPathSpecFromConfig(t *testing.T) {
require.True(t, p.disablePathToLower)
require.True(t, p.multilingual)
require.True(t, p.removePathAccents)
require.True(t, p.trimTrailingSlash)
require.True(t, p.uglyURLs)
require.Equal(t, "no", p.defaultContentLanguage)
require.Equal(t, "no", p.language.Lang)
Expand Down
8 changes: 6 additions & 2 deletions helpers/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,18 @@ func (p *PathSpec) URLizeAndPrep(in string) string {

// URLPrep applies misc sanitation to the given URL.
func (p *PathSpec) URLPrep(in string) string {
if p.uglyURLs {
if p.uglyURLs && !p.trimTrailingSlash {
return Uglify(SanitizeURL(in))
}
pretty := PrettifyURL(SanitizeURL(in))
if path.Ext(pretty) == ".xml" {
return pretty
}
url, err := purell.NormalizeURLString(pretty, purell.FlagAddTrailingSlash)
flag := purell.FlagAddTrailingSlash
if p.trimTrailingSlash {
flag = purell.FlagRemoveTrailingSlash
}
url, err := purell.NormalizeURLString(pretty, flag)
if err != nil {
return pretty
}
Expand Down
27 changes: 21 additions & 6 deletions helpers/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,19 +241,34 @@ func TestMakePermalink(t *testing.T) {

func TestURLPrep(t *testing.T) {
type test struct {
ugly bool
input string
output string
trimTrailingSlash bool
uglyURLs bool
input string
output string
}

data := []test{
{false, "/section/name.html", "/section/name/"},
{true, "/section/name/index.html", "/section/name.html"},
// trimTrailingSlash=false, uglyURLs=false
{false, false, "/section/name.html", "/section/name/"},
{false, false, "/section/name/index.html", "/section/name/"},

// trimTrailingSlash=false, uglyURLs=true
{false, true, "/section/name.html", "/section/name.html"},
{false, true, "/section/name/index.html", "/section/name.html"},

// trimTrailingSlash=true, uglyURLs=false
{true, false, "/section/name.html", "/section/name"},
{true, false, "/section/name/index.html", "/section/name"},

// trimTrailingSlash=true, uglyURLs=true
{true, true, "/section/name.html", "/section/name"},
{true, true, "/section/name/index.html", "/section/name"},
}

for i, d := range data {
v := viper.New()
v.Set("uglyURLs", d.ugly)
v.Set("trimTrailingSlash", d.trimTrailingSlash)
v.Set("uglyURLs", d.uglyURLs)
l := NewDefaultLanguage(v)
p, _ := NewPathSpec(hugofs.NewMem(v), l)

Expand Down
1 change: 1 addition & 0 deletions hugolib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func loadDefaultSettingsFor(v *viper.Viper) error {
v.SetDefault("ignoreCache", false)
v.SetDefault("canonifyURLs", false)
v.SetDefault("relativeURLs", false)
v.SetDefault("trimTrailingSlash", false)
v.SetDefault("removePathAccents", false)
v.SetDefault("titleCaseStyle", "AP")
v.SetDefault("taxonomies", map[string]string{"tag": "tags", "category": "categories"})
Expand Down
Loading