Run site from subdirectory (in localhost + live web server)


Building my first site with Hugo - basically I’m migrating a blog from Blogger.
I followed the documentation and set up a new site as explained, with the default directory structure. Installed a theme (“mainroad”) and put all my content in place. So far everything works beautifully from localhost:1313 (running the Hugo web server locally on Windows).

But here’s the problem: I’m not going to publish the site to my domain root – the location will be “/blog”. No matter what I try, I can’t get Hugo to successfully set up this structure on localhost.

  • If I set BaseURL in my config to “/blog”, Hugo says Web Server is available at //localhost:1313///blog/. But all get there is “404 not found”, even if I remove “/blog” from the accessed URL.
  • If I try to additionally modify the directory structure, I either get more 404s or Hugo cannot find the content for building the site.
  • I also tried playing with relativeURLs and canonifyURLs, but no luck.
  • Tried using different themes just for testing - I get the same symptoms with all of them so I’m confident it’s not a theme-specific issue.

The Hugo docs don’t cover this situation (they seem to assume that the site will be at the domain root). Strangely enough I can’t find any relevant discussion either, except for threads like this… with encouraging things like “Figured out!” and “It works, don’t worry”, but no actual explanation.

So for anyone who has their site running off a subdirectory: How do you do it? And how should I set it up locally, in terms of directory structure and config parameters?

BTW, here’s my environment if it helps any:

Hugo Static Site Generator v0.37.1 windows/amd64 BuildDate: 2018-03-07T17:56:57Z
hugo server -b localhost/foo/ 

Also, in general, setting canonifyURLs = true works better in my experience when dealing with baseURLs with subdirs.

There was a recent thread where @it-gro and I got into indepth analysis of relURL, absURL, canonifyURLs with such baseURLs.

1 Like

Thank you - a baseURL of localhost/blog/ works fine. I suppose it’s a bit counter-intuitive that just /blog/ doesn’t work with the Hugo webserver.

A minor inconvenience is that this adds “localhost” to the URLs in all my static pages, so I’d have to change config options between my local and published versions. Thankfully I just found (here) that I can set my baseURL to “//domain.tld/blog/”, which removes the domain name when the pages are generated. So that’s taken care of. :slight_smile:

The change also broke all static file links (images etc.) in my blog post contents, but I’ll try fixing that with custom shortcodes as described in this post.

Thanks for the indepth discussion link as well!