Hugo: `hugo server` not serving subdirectories

Created on 13 Jan 2015  Â·  12Comments  Â·  Source: gohugoio/hugo

Hi,

Thanks for hugo: it's a fantastic change from tools like DocPad, which takes minutes (minutes!) to build a not-terribly-complex academic website.

I'm having trouble, however, using hugo server with a baseurl that includes subdirectories. Basically, everything but the front page gets a 404, even though I can see all of the content in the generated public directory.

I can reproduce this with a pretty simple configuration, a lightly modified version of what hugo create site gives me. Here is my (reduced) config.toml file:

baseurl = "http://yourSiteHere/~username/foo/bar"
languageCode = "en-us"
title = "my new hugo site"

I have one piece of content, in content/blog/post.md:

+++
date = "2015-01-01T20:28:38-03:30"
title = "post"

+++
This is the first blog post.

My layouts/index.html file looks like this:

<html>
    <head><title>{{ .Site.Title }}</title></head>
    <body>
        <h1>{{ .Site.Title }}</h1>

        <h2>Links:</h2>
        <ul><li><a href="blog/post">First blog post</a></li></ul>
    </body>
</html>

When I just run hugo server, it generates all of the expected files in the public directory:

public/404.html
public/blog/index.html
public/blog/index.xml
public/blog/post/index.html
public/index.html
public/index.xml
public/sitemap.xml

and the sitemap is correct:

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

  <url>
    <loc>http://localhost:1313/~username/foo/bar/</loc>
    <lastmod>2015-01-01T20:28:38-03:30</lastmod>
    <priority>0</priority>
  </url>

  <url>
    <loc>http://localhost:1313/~username/foo/bar/blog/post/</loc>
    <lastmod>2015-01-01T20:28:38-03:30</lastmod>
  </url>

</urlset>

but the only URL that Hugo actually serves is http://localhost:1313/~username/foo/bar. All of the following URLs result in 404 errors:

http://localhost:1313/~username/foo/bar/
http://localhost:1313/~username/foo/bar/blog
http://localhost:1313/~username/foo/bar/blog/
http://localhost:1313/~username/foo/bar/blog/index.html
http://localhost:1313/blog
http://localhost:1313/blog/

Any idea what might be wrong or how it can be fixed?

Bug Docs

Most helpful comment

Hi @trombonehero,

You wrote earlier that:

I can reproduce this with a pretty simple configuration, a lightly modified version of what hugo create site gives me. Here is my (reduced) config.toml file:

baseurl = "http://yourSiteHere/~username/foo/bar"
languageCode = "en-us"
title = "my new hugo site"

Sorry, I wasn't reading carefully and didn't notice the lack of a trailing slash (/) in your baseurl, whereas I added a trailing slash to mine, as in:

baseurl = "http://yourSiteHere/~username/foo/bar/"

When I remove the trailing slash, I could reproduce your error, and see that even the stylesheet isn't properly loaded.

Note, however, as you may have already discovered, that this only affects hugo server. If you just run hugo -t redlounge without the server subcommand, the site that is generated in the public/ directory seems perfectly normal when run on your destination server.

Thank you, @trombonehero, I think you may have found a bug in the server subcommand!

Suggested TODO list for Hugo developers:

  1. Document this behaviour, especially for v0.12 users, telling the user to make sure baseurl ends with a trailing slash.
  2. Make hugo server smarter by adding the trailing slash if necessary, and warn the user of the missing trailing slash.

All 12 comments

What version do you use? (the output of hugo server).

I'm using v13, installed via Homebrew:

Hugo Static Site Generator v0.13-DEV buildDate: 2015-01-10T18:24:52-03:30

Hello @trombonehero,

It might have to do with the theme. I just did a quick test with the Red Lounge theme which seems to work. That said, some themes might have partials with hard-coded paths without regard for baseurl. So please play around with other themes and see if some of them work for you.

Which theme are you using, by the way?

Hi, and thanks for your responses so far... this seems to be a pretty
active community!

I'm actually not using any theme at all for either my site or the
reduced test case. I just tried cloning and using redlounge, but I get
the same 404 page not found error when I try to visit
/~username/foo/blog/post.

Am I doing something wrong in the configuration that's different from
what you're using?

Anthony Fok mailto:[email protected]
January 12, 2015 at 9:37 PM

Hello @trombonehero https://github.com/trombonehero,

It might have to do with the theme. I just did a quick test with the
Red Lounge https://github.com/tmaiaroto/hugo-redlounge theme which
seems to work. That said, some themes might have partials with
hard-coded paths without regard for baseurl. So please play around
with other themes and see if some of them work for you.

Which theme are you using, by the way?

—
Reply to this email directly or view it on GitHub
https://github.com/spf13/hugo/issues/793#issuecomment-69678434.

Hi @trombonehero,

You wrote earlier that:

I can reproduce this with a pretty simple configuration, a lightly modified version of what hugo create site gives me. Here is my (reduced) config.toml file:

baseurl = "http://yourSiteHere/~username/foo/bar"
languageCode = "en-us"
title = "my new hugo site"

Sorry, I wasn't reading carefully and didn't notice the lack of a trailing slash (/) in your baseurl, whereas I added a trailing slash to mine, as in:

baseurl = "http://yourSiteHere/~username/foo/bar/"

When I remove the trailing slash, I could reproduce your error, and see that even the stylesheet isn't properly loaded.

Note, however, as you may have already discovered, that this only affects hugo server. If you just run hugo -t redlounge without the server subcommand, the site that is generated in the public/ directory seems perfectly normal when run on your destination server.

Thank you, @trombonehero, I think you may have found a bug in the server subcommand!

Suggested TODO list for Hugo developers:

  1. Document this behaviour, especially for v0.12 users, telling the user to make sure baseurl ends with a trailing slash.
  2. Make hugo server smarter by adding the trailing slash if necessary, and warn the user of the missing trailing slash.

Hi @anthonyfok,

Thanks for your help. It looks like a pretty clear case of PEBKAC on my end, but I agree that some documentation and a warning message wouldn't go astray. Even better, as you say, would be if Hugo normalized baseurl into whatever format it needs internally.

Anyway, my site is now working consistently between hugo and hugo server, so I no longer need to change config.yaml every time I compile for development vs deployment. Thanks again!

Jon

We will fix it.

And, if I read your nick (trombonehero) correctly, we might create a new slogan: "Hugo - for horn players!". Yes, it's me with the saxophone in my avatar.

For reference, a duplicate bug report has just been filed by @bguiz in Issue #799: "hugo server when BaseUrl has path does not serve any pages other than /".

The fix turns out to be easier than I thought, as there was already a fixUrl() in hugo/commands/server.go function purely for massaging that baseUrl into a form suitable for serving webpages locally, e.g. adding a missing http:// prefix, so it was easy to add a missing / suffix too. ;-)

The fix is committed in 1cbdd3e. @trombonehero and @bguiz, please test and see it works for you now.

Sidenote: The fact that several users have run into this problem is probably because they are used to Jekyll's norm of leaving out the trailing slash for baseurl, as evident in this paragraph in Jekyll documentation on http://jekyllrb.com/docs/github-pages/:

In _config.yml, set the baseurl option to /project-name – note the leading slash and the absence of a trailing slash.

Here is a reminder to myself to add a note about the trailing / in docs/content/overview/configuration.md.

Cheers,
Anthony

Hi @anthonyfok thanks for looking into this.

I assumed that your commits have been merged into master, so I did this:

go get -v github.com/spf13/hugo

Now with the latest hugo, I run the server again, and I still have the same issue. Trailing slash or otherwise - so it does not appear to have fixed the issue.

Also, I am facing issues with BaseUrl and src attributes, which may be related to this one:

https://github.com/spf13/hugo/issues/803

@bguiz was it -v or -u that you did?

You can otherwise just cd to the hugo directory and do a git pull --rebase.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mumblecrunch picture mumblecrunch  Â·  3Comments

tjamet picture tjamet  Â·  3Comments

carandraug picture carandraug  Â·  3Comments

antifuchs picture antifuchs  Â·  3Comments

VoidingWarranties picture VoidingWarranties  Â·  3Comments