You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Nginx.conf实现.html与index.html的URL重定向

Fixing Nginx URL Redirects for Clean URLs

Hey there, let's work through your Nginx redirect requirements to get those clean, user-friendly URLs working without loops or unexpected behavior. First, let's restate your core goals to make sure we're aligned:

  • Redirect mysite.com/folder/index.html to mysite.com/folder/
  • Redirect mysite.com/folder.html to mysite.com/folder/
  • Redirect mysite.com/index.html to mysite.com

Let's break down the issues with your previous attempts first, then jump to the working configuration.

What Was Wrong With Previous Configs?

  1. First rewrite-based setup: The rewrite rules are on the right track, but they lack explicit permanent redirects (critical for SEO) and their ordering could lead to unexpected conflicts with the try_files directive.
  2. Second @htmlext setup: This does the opposite of what you need—it rewrites clean URLs to .html files instead of redirecting .html requests away from them.
  3. Third regex location setup: The broad regex patterns cause Nginx to match the redirected clean URL (like /folder/) back against the location blocks, triggering an infinite redirect loop (ERROR_LOOP).

Working Nginx Configuration

Here's a tested, loop-free configuration that meets all your requirements, with explanations for each part:

server_name k.my.net www.k.my.net;
index index.html;
root /var/www/demo/k.my.net/current/public;

# Redirect /index.html to root (mysite.com)
rewrite ^/index\.html$ / permanent;

# Redirect /folder/index.html to /folder/
rewrite ^(.+/index)\.html$ $1/ permanent;

# Redirect /folder.html to /folder/
rewrite ^(.+)\.html$ $1/ permanent;

location / {
    try_files $uri $uri/ =404;
}

Why This Works:

  • Permanent redirects: Using permanent sends a 301 status code, which tells search engines to update their indexes to the clean URL (great for long-term SEO).
  • Specific regex ordering: Rules are written from most specific to least specific:
    1. First handle the root index.html redirect to avoid it being caught by the broader folder rule.
    2. Next target subfolder index.html files, ensuring they redirect to the trailing-slash folder URL.
    3. Finally, catch any remaining .html files (like folder.html) and redirect them to their trailing-slash equivalent.
  • No loop risk: Once redirected to the clean URL (e.g., /folder/), the try_files directive serves the corresponding index.html or .html file without triggering the rewrite rules again—since the new URL doesn't end with .html.

Alternative: Using return Instead of rewrite (More Efficient)

If you prefer Nginx's lighter, more efficient return directive over rewrite, here's an equivalent version:

server_name k.my.net www.k.my.net;
index index.html;
root /var/www/demo/k.my.net/current/public;

# Redirect root index.html to / (exact match, most efficient)
location = /index.html {
    return 301 /;
}

# Redirect subfolder index.html to folder/
location ~ ^(.+/index)\.html$ {
    return 301 $1/;
}

# Redirect any .html file to its trailing-slash counterpart
location ~ ^(.+)\.html$ {
    return 301 $1/;
}

location / {
    try_files $uri $uri/ =404;
}

The = modifier in the first location block creates an exact match, which is the fastest way to handle the root index.html redirect.

内容的提问来源于stack exchange,提问作者Nick Medvik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:28:04