Architecture · Guides

Clean URLs on Nginx without a redirect loop: the config and the bug.

How to serve /about/ and /blog/post instead of .html addresses with Nginx try_files, which 301 rules to add and the 404.html redirect loop that caused 500.

Key takeaways

  • Use try_files $uri $uri/ $uri.html =404; to serve page.html at /page.
  • Redirect the .html versions to the clean ones with a 301, so there is one canonical URL.
  • Exclude your 404.html from the redirect, or errors can loop and return a 500.
  • Nginx has no .htaccess: the rules go in the server {} block.

The goal

Every page on this site is an .html file, but the addresses people see have no extension: /about/ instead of /about/index.html, and /blog/some-post instead of /blog/some-post.html. Clean URLs are shorter, easier to share and better for consistent canonical links. They also avoid having the same page reachable at two addresses.

Apache versus Nginx

On Apache, this lives in a .htaccess file next to your pages. Nginx has no per-directory config file, so the rules go in the site's server {} block. If you use a panel such as aaPanel, paste them into the site's config, alongside whatever the panel generated for PHP, SSL and logs, and reload Nginx.

The rules

1. Redirect /foo/index.html to /foo/

if ($request_uri ~ ^/(.*)index\.html(\?.*)?$) {
    return 301 /$1$2;
}

2. Redirect /blog/post.html to /blog/post

if ($request_uri ~ "^/(?!404\.html)(.+)\.html(\?.*)?$") {
    return 301 /$1$2;
}

Note the negative lookahead (?!404\.html). That is the fix for the bug below.

3. Serve the right file for clean URLs

location / {
    try_files $uri $uri/ $uri.html =404;
}
error_page 404 /404.html;

try_files tries the exact path, then a directory index, then the path with .html appended, and finally returns a real 404 status.

4. Keep the error page internal

location = /404.html {
    internal;
}

The bug I caused

My first version stripped .html from every URL, including the internal /404.html fallback. This is what happened when someone hit a broken link:

  1. Nginx could not find the page and internally requested /404.html.
  2. The redirect rule matched /404.html and sent a 301 to /404.
  3. /404 was not found, which again requested /404.html.
  4. Nginx detected the redirect cycle and aborted with a raw 500 Internal Server Error.

Every broken link on the site returned a 500 instead of my branded 404 page. Visitors saw an error, and search engines would see the wrong status.

The fix was twofold: the negative lookahead that excludes 404.html from the redirect, and marking the file internal, so nobody can request it directly and loop.

Testing it

Check the actual status codes, not just the pages. For example:

curl -I https://example.com/about/index.html   # 301 to /about/
curl -I https://example.com/blog/post.html     # 301 to /blog/post
curl -I https://example.com/blog/post          # 200
curl -I https://example.com/no-such-page       # 404, with the branded page

A browser can hide redirect chains, so use curl -I or a similar tool to see the real codes.

Keep a changelog in the config

I keep a short version history in comments at the top of the Nginx file: what changed, what broke and why. It reads like a changelog for infrastructure, and it has stopped me repeating a mistake more than once.

SEO notes

  • Make sure your <link rel="canonical"> uses the clean URL.
  • List clean URLs in the sitemap.
  • Use 301, not 302, so search engines consolidate the old address into the new one.

Frequently asked questions

Why not use rewrite instead of try_files?

try_files is simpler and avoids extra rewrite processing for static files. Use rewrite when you need more complex pattern changes.

Will clean URLs hurt rankings?

No. Done with 301 redirects and consistent canonicals, they consolidate signals on one address.

What about trailing slashes?

Pick one style and be consistent. Directory index pages end with a slash, and single files such as blog posts do not, as on this site.

Running a static site on your own VPS and want it set up properly?

Get in touch