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

NGINX - Rewrite version number in JS file name to parameter for PHP

· 52CE 我爱测

I had a rewrite from file.js?c=123 to file.js.php?c=123 but query params were downgrading my SEO evaluation. Actually, some proxy CDN do not cache query params. server { location ~ \startscript.js$ { rewrite (.*) $1.php last; } }…

问题原文

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

I had a rewrite from file.js?c=123 to file.js.php?c=123 but query params were downgrading my SEO evaluation. Actually, some proxy CDN do not cache query params.

server {
    location ~ \startscript.js$ {
        rewrite (.*) $1.php last;
    }
}

SO, I would like to rewrite file.123.js to file.js.php?c=123. Is that possible?

已采纳解答

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

location ~ \startscript.js$ {

In regex the \s at the start of your uri is a shorthand character class that matches a single space (or "whitespace character"). So this is unlikely to have matched anything. However, you should backslash escape the literal dot before the file extension.

I assume "file" in file.js could be any file, it's not always "startscript"?

Try the following instead (UPDATED):

server {
    location ~ \.js$ {
        rewrite (.+)\.(\d+)\.js$ $1.js.php?c=$2 last;
    }
}

However, as I mentioned in comments, this makes no difference with regards to SEO (search engines handle the query string just fine - always have). In the past, it was possibly a usability issue, as some proxy servers did not cache based on the query string - so users might not have seen the correct content (if they were behind a proxy that behaved this way) - but this is historical AFAIK.

See also my answer to the following question:


... this is throwing 404 error, Eg: domain.com/startscript.82b4527379bd2540f79f532950c7b96b.js

The above example specifically matches digits only (as in your initial example). What you have there looks like a 32 char hex value (hash?). Try the following instead (UPDATED):

rewrite "(.+)\.([\da-f]{32})\.js$" $1.js.php?c=$2 last;

Or use the the more generic \w+, as @devnull suggested in comments, to match any "word" character (ie. 0-9, a-z, A-Z and _). Although with regex, it's generally advisable to be as specific as possible, so if it's always a 32 char hex then match a 32 char hex.

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

资料来源与适用范围

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

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

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