大网数据广告活动
网站运营与故障问答

Nginx URL rewrite with static .html files for SEO

· 52CE 我爱测

I'm trying to setup a Nginx configuration for nice URLs without .html extension. What I currently have works, but the / URI redirects to /index and I don't want that. The URL rewrite rules should be as follows: /index.html -> / /…

问题原文

原帖链接 · Rumplin · CC BY-SA 4.0

I'm trying to setup a Nginx configuration for nice URLs without .html extension.

What I currently have works, but the / URI redirects to /index and I don't want that.

The URL rewrite rules should be as follows:

/index.html -> /
/test.html -> /test
/subpage/ -> /subpage
/subpage/test.html -> /subpage/test
/sub/sub/index.html -> /sub/sub
/sub/sub/ -> /sub/sub
/somepage/ -> /somepage
/somepage.html -> /somepage

Here is the configuration:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name _;

    set $root /app/www/public;
    if (!-d /app/www/public) {
        set $root /config/www;
    }
    root $root;
    index index.html;
    
    error_page 401 /401;
    error_page 403 /403;
    error_page 404 /404;
    error_page 500 502 503 504 /500;
    
    location / {
        #redirect index.html to /
        if ( $request_uri ~ "index.html" ) {
            rewrite ^(.*)/ $1/ permanent;
        }

        #remove .html from request
        rewrite ^/(.*)\.html$ /$1 permanent;
        
        #remove the trailing /
        rewrite ^/(.*)/$ /$1 permanent;

        try_files $uri.html $uri/ $uri =404;
    }
    
    location ~* \.(jpg|jpeg|png|gif|ico)$ {
       expires 30d;
    }
    location ~* \.(css|js)$ {
       expires 7d;
    }

    # deny access to .htaccess/.htpasswd files
    location ~ /\.ht {
        deny all;
    }
}

NOTE: this Nginx is running in a docker container.

已采纳解答

原帖链接 · Stephen Ostermiller · CC BY-SA 4.0

I got it to work with this configuration which I tested on nginx/1.18.0 (Ubuntu):

    index XXXXXX.DISABLE.INDEX.DOCUMENT.XXXXXX;
    location / {
        #remove index.html from request
        rewrite ^/(.*)/index(\.html)?$ /$1 permanent;    
        rewrite ^/(.*)index(\.html)?$ /$1 permanent;        

        #remove .html from request
        rewrite ^/(.*)\.html$ /$1 permanent;
        
        #remove the trailing /
        rewrite ^/(.*)/$ /$1 permanent;
        
        try_files $uri ${uri}/index.html ${uri}.html =404;
    }

By disabling the index functionality, you can handle add index.html to the try_files which is more compatible with the rewrite rules.

You can control if /sub/index.html or /sub.html takes precedence for a request for /sub by changing the order of ${uri}/index.html and ${uri}.html in try_files.

Tests:

  • curl --head http://localhost/ -> 200 OK (powered by /index.html)
  • curl --head http://localhost/index -> 301 Moved Permanently to Location: http://localhost/
  • curl --head http://localhost/index.html -> 301 Moved Permanently to Location: http://localhost/
  • curl --head http://localhost/page -> 200 OK (powered by /page.html)
  • curl --head http://localhost/page.html -> 301 Moved Permanently to Location: http://localhost/page
  • curl --head http://localhost/sub -> 200 OK (powered by /sub/index.html)
  • curl --head http://localhost/sub/ -> 301 Moved Permanently to Location: http://localhost/sub
  • curl --head http://localhost/sub/ -> 301 Moved Permanently to Location: http://localhost/sub
  • curl --head http://localhost/sub/index -> 301 Moved Permanently to Location: http://localhost/sub
  • curl --head http://localhost/sub/index.html -> 301 Moved Permanently to Location: http://localhost/sub

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2022-09-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。