1

We're generating a site consisting of only static files (using Assemble).

Having the .html extension on URLs looks so nineties, so we generate every static HTML file in its own directory and call it index.html. For example, the url http://www.example.com/foo/bar/ is in the file /var/www/foo/bar/index.html.

This works well, but there is one small thing nagging me: Now there are two possible URLs to the same resource:

  1. http://www.example.com/foo/bar/ (slash URL)
  2. http://www.example.com/foo/bar/index.html (index.html URL)

By accident someone may link to the index.html form of the URL, which is bad for SEO and looks ugly (remember the nineties?).

Is it possible in Nginx to give a 404 error on the index.html URL, but serve the slash URL?

I tried this:

location ~ /index\.html$ {
    return 404;
}

But it seems that Nginx does some internal rewrite of the slash URL to the index.html URL, and then matches this location so we get a 404 even on the slash URL.

Note that to catch mistakes, we want index.html URLs to be an error, not just redirect to the slash URL.

2
  • 1
    Do you have any evidence that index.html is actually bad for SEO? Search engines have been quite good at figuring out that sort of non-malicious duplicate content situation and picking the canonical URL for years now (as they have with www vs. non-www).
    – ceejayoz
    Oct 23, 2013 at 12:05
  • Nope, that's just something I heard. It's bad for caching in any case. Oct 24, 2013 at 9:50

1 Answer 1

0

There is an internal redirect for index.html as it is the default index; the / request redirects to /index.html which is set to return 404.

You can check the ngx_http_index_module documentation to get a better understanding. Your case is documented.

An approach for your issue would be to rename all index.html to foo.bar and add that filename as default index

location / {
    index foo.bar;
}
1
  • It's a bit of a hack, but that should work. Nobody links to foo.bar by accident. :-) Oct 24, 2013 at 11:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .