Apache 通过 .htaccess 配置前端路由的静态网站

1<IfModule mod_rewrite.c>
2  	RewriteEngine On
3    RewriteBase /
4
5    # 1. 如果访问的是 /about,且服务器存在 about.html,则自动映射
6    # 但不要在浏览器地址栏改变 URL(保持无后缀状态)
7    RewriteCond %{REQUEST_FILENAME} !-d
8    RewriteCond %{REQUEST_FILENAME}.html -f
9    RewriteRule ^([^/]+)$ $1.html [L]
10
11    # 2. 如果访问的是具体的 .html 文件(如 about.html),强制跳转到无后缀版本
12    # 这样可以统一 URL 规范,有利于 SEO
13    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
14    RewriteRule ^(.*)\.html$ /$1 [R=301,L]
15
16    # 3. 你的原有单页应用路由逻辑(针对不存在的文件)
17    RewriteCond %{REQUEST_FILENAME} !-f
18    RewriteCond %{REQUEST_FILENAME} !-d
19    RewriteRule . /index.html [L,QSA]
20</IfModule>