Help needed to redirect /index.html for all pages to their root

when you visit my website https://www.wheelchairway.com the same URL https://www.wheelchairway.com/index.html is also available, this is for all pages on the website.

Going by search console standards the index.html has the same content as the root UR so this is considered duplicate content. I already have setup a canonical tag in the header but the index.html is still available for people to visit.

Because I use hugo to render my website with Netlify as the backend I was looking for a way to redirect the index.html back to the root of each page.

I like to know if Hugo has this type of function build in.

Something similar like the PHP version like this:

RewriteEngine On
RewriteRule ^index.html$ / [R=301,L]
RewriteRule ^(.*)/index.html$ /$1/ [R=301,L]

Hope that this gives a better understanding of what I am looking to solve.

That “similar to PHP” is “how it’s done on an Apache webserver”.

Netlify has a setting in the deploy section (I think) where you set “fix ugly urls”. blabla/index.html would be “ugly” and will be corrected into blabla/.

Go check out all settings in Netlify. That should do the trick. If not, google _redirects in Netlify. That’s a file you put into the static folder where you redirect stuff. But I am pretty sure the ugly setting does that automatic.

By the way: if you are concerned about Google and SEO, then rest assured that blabla/ is understood by Google to be the same as blabla/index.html. No worries there.

True that netlify has the pretty URL setting in the build setting but this is only for how the url looks it will not redirect the /index.html back to the root with a 301 redirect.

i know Google will see it as the same and will pick the one you specified in the canonical tag, but if you have a running site the /index.html can contain links pointing back to your site, so for SEO it’s a good practice to get all the link pointing to the correct URL.

I think I will manually redirect the /index.html to the root with the _redirects file you talked about earlier.

Just hope there was a easier and more automated way of doing this.

At least thanks for pointing me in the richt direction.

[[redirects]]
from = "/*/index.html"
to = "/:splat:"
status = 200
force = false

or

/*/index.html /:splat: 200

should do the trick. first one in netlify.toml, second one in _redirects.

2 Likes

that did the trick, thank you so much for all the trouble.

An HTTP request for / will make most web servers check for a index.html, index.htm or index.php file and serve it. Replying to e.g. /index.html with a redirect to / will create a loop, although browsers typically give up after several redirects in a row. Anyway, you should never do that. What you actually want is URL rewriting (i.e. serve content even if the request is not asking exactly for the correct resource or different one judging by the URL).

Replying to e.g. /index.html with a redirect to / will create a loop, although browsers typically give up after several redirects in a row.

No. You mix up things here. There is no loop. A server serves the file it is configured to serve when the request ends in an / and not a real existing file name. The URL stays the same, there is no redirect. There are no multiple requests to the server from the browser. Because the server interprets the URL and delivers the right content. The browser is not doing anything else than requesting the resource and showing what’s returned.

1 Like

If the server interprets / as a request for /index.html (internal redirect) but a request for /index.html is answered with a redirect to /, then that would result in / → /. A server could reply to GET /index.html with a redirect to / and serve whatever content for that request of course. It depends on the configuration.

Once again, you misunderstand how a webserver works. Let’s not go deeper on this as it has nothing to do with this OT. The server does not “interpret”, he “serves” an index.htm (or any other configured file) when / is requested. he does not redirect because index.htm is set as directory index file.

1 Like