部署单页应用时,客户端路由和文件在服务器上的位置可能不一致。例如某页面客户端路由为/crdt,但对应的html文件在服务器上存放的位置可能是 /index.html/crdt.html

一般的CDN或对象存储提供了自动从 / 跳转到 /index.html 的能力,但是对于单页应用,还需要支持从 /<slug> 跳转到 /<slug>.html/index.html 的能力

使用阿里云全站加速 DCDN时,可使用以下脚本实现路径重写,实现客户端路由到服务端路由的自动转换,避免回源时出现404

lut = []
set(lut,'.canvas', true)
set(lut,'.css', true)
set(lut,'.html', true)
set(lut,'.js', true)
set(lut,'.json', true)
set(lut,'.png', true)
set(lut,'.xml', true)
set(lut,'.jpeg', true)
set(lut,'.jpg', true)
set(lut,'.gif', true)
ext = req_uri_ext()
if ne(get(lut,ext),true) {
 
	if eq(req_uri(),'/') {
		rewrite('/index.html', 'break')
	} else {
		rewrite(concat(req_uri(), '.html'), 'break')
	}
}