如何配置Nginx.conf实现.html与index.html的URL重定向
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.htmltomysite.com/folder/ - Redirect
mysite.com/folder.htmltomysite.com/folder/ - Redirect
mysite.com/index.htmltomysite.com
Let's break down the issues with your previous attempts first, then jump to the working configuration.
What Was Wrong With Previous Configs?
- 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_filesdirective. - Second
@htmlextsetup: This does the opposite of what you need—it rewrites clean URLs to.htmlfiles instead of redirecting.htmlrequests away from them. - 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
permanentsends 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:
- First handle the root
index.htmlredirect to avoid it being caught by the broader folder rule. - Next target subfolder
index.htmlfiles, ensuring they redirect to the trailing-slash folder URL. - Finally, catch any remaining
.htmlfiles (likefolder.html) and redirect them to their trailing-slash equivalent.
- First handle the root
- No loop risk: Once redirected to the clean URL (e.g.,
/folder/), thetry_filesdirective serves the correspondingindex.htmlor.htmlfile 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

