You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Proposal: Add an option to remap files named <dir>/index.html to <dir>/ during deployment.
Background
Traditional web servers (e.g. Apache, nginx) are typically configured to serve index.html files when users visit a directory. However, blob storage systems (e.g. S3, GCS) let you create files with names ending in /, and this is sometimes preferred.
For example, let’s say you deploy your site to an S3 bucket and CloudFront distribution. The simplest way to do this is to just use an S3 origin for your CloudFront distribution. However, this simple setup doesn’t work with Hugo out of the box—your website root / will correctly map to the root index.html file, but subdirectories will not work. You will get a 404 error for dir/, because CloudFront will look for an S3 object named dir/ instead of dir/index.html.
There are some workarounds for this:
You can enable “static website hosting” for your S3 bucket, set the index document to index.html, and configure CloudFront to use an HTTP origin that points to the S3 bucket. However, this prevents you from using the normal authentication & authorization systems that you would otherwise get, so this option only works for sites that are public.
You can use a CloudFront function to remap requests to dir/ to request dir/index.html, but this adds complexity and code to what would otherwise be a no-code configuration. See: https://stackoverflow.com/a/76581267
Solution
Add a new option to the deployment.targets section of the configuration called stripIndexHTML. This option maps any local file named <dir>/index.html to a remote file named <dir>/. The root page, index.html, is untouched.
This is “elegant” because this just means that the remote file path matches the canonical URLs that Hugo uses anyways. Your link to dir/ maps to a remote file named dir/.
Impact
From a web search, I can see that other people using Hugo have experienced this problem:
discourse.gohugo.io: “Index.html pages in S3 bucket aren’t loading through CloudFront distribution”
Stack Overflow: “Hugo on S3 with CloudFront doesn't redirect to index.html.”
Stack Overflow: “Hugo multilingual site deploy to aws s3 error ‘AccessDenied’”
Stack Overflow: “AWS static links fail without ‘index.html’”
The text was updated successfully, but these errors were encountered:
This new configuration parameter causes paths matching
"<dir>/index.html" to be stored as "<dir>/" remotely. This simplifies
the cloud configuration needed for some use cases, such as CloudFront
distributions with S3 bucket origins. Before this change, users must
configure their S3 buckets as public websites (which is incompatible
with certain authentication / authorization schemes), or users must add
a CloudFormation function to add index.html to the end of incoming
requests. After this change, users can simply use an ordinary CloudFront
distribution (no additional code) with an ordinary S3 bucket origin (and
not an S3 website).
This adds tests to ensure that functionality like matchers is unaffected
by this change. I have also tested that the functionality works as
expected when deploying to a real S3 / CloudFront website.
Closesgohugoio#12607
Proposal: Add an option to remap files named
<dir>/index.html
to<dir>/
during deployment.Background
Traditional web servers (e.g. Apache, nginx) are typically configured to serve
index.html
files when users visit a directory. However, blob storage systems (e.g. S3, GCS) let you create files with names ending in/
, and this is sometimes preferred.For example, let’s say you deploy your site to an S3 bucket and CloudFront distribution. The simplest way to do this is to just use an S3 origin for your CloudFront distribution. However, this simple setup doesn’t work with Hugo out of the box—your website root
/
will correctly map to the rootindex.html
file, but subdirectories will not work. You will get a 404 error fordir/
, because CloudFront will look for an S3 object nameddir/
instead ofdir/index.html
.There are some workarounds for this:
You can enable “static website hosting” for your S3 bucket, set the index document to
index.html
, and configure CloudFront to use an HTTP origin that points to the S3 bucket. However, this prevents you from using the normal authentication & authorization systems that you would otherwise get, so this option only works for sites that are public.You can use a CloudFront function to remap requests to
dir/
to requestdir/index.html
, but this adds complexity and code to what would otherwise be a no-code configuration. See: https://stackoverflow.com/a/76581267Solution
Add a new option to the
deployment.targets
section of the configuration calledstripIndexHTML
. This option maps any local file named<dir>/index.html
to a remote file named<dir>/
. The root page,index.html
, is untouched.This is “elegant” because this just means that the remote file path matches the canonical URLs that Hugo uses anyways. Your link to
dir/
maps to a remote file nameddir/
.Impact
From a web search, I can see that other people using Hugo have experienced this problem:
The text was updated successfully, but these errors were encountered: