【发布时间】:2018-08-31 11:20:40
【问题描述】:
我想为 Nginx 编写一个重写规则,以将任何对 javascript 文件的请求重定向到特定的 JS 文件。例如“http://example.com/path1/path2/sample.js?a=1&b=2”应重定向到“http://example.com/myjavascript.js”。所以我将此规则添加到 /etc/nginx/sites-enabled/example.com 配置文件:
rewrite .*\.js.* myjavascript.js last;
因此任何包含 .js 的 URL 都应替换为 myjavascript.js。但是当我尝试使用 URL 'http://example.com/path1/path2/sample.js?a=1&b=2' 时,它会失败(404)。但是,奇怪的是,如果我将 js 文件替换为 HTML 文件,例如:
rewrite .*\.js.* /hello.html last;
它正确地重定向它。 myjavascript.js 与 hello.html 位于同一文件夹中,但它没有将其正确重定向到此路径。
你能告诉我为什么它不起作用吗?
【问题讨论】:
标签: nginx mod-rewrite url-rewriting url-redirection