问题原文
原帖链接 · fiskolin · CC BY-SA 4.0
I have a dynamically created file start.js.php?c=21 and I would like to rewrite it's extension (to start.js, with query params).
- Is that possible?
- Is that a good method having such a rewrite?
- Is wrong having query string
?c=21on file location (to populate from PHP file with givencattribute)?
I am trying to create rewrite with:
server {
location ~ \.js.php$ {
rewrite ^(.*)$ $1.js last;
}
}
This is returning 404 error code.
已采纳解答
原帖链接 · MrWhite · CC BY-SA 4.0
If the underlying filename is script.js.php then it doesn't make sense to rewrite this to script.js - as that would result in a 404.
However, the directives you posted would seem to result in a 404 for a different reason, as they rewrite script.js.php to script.js.php.js.
Presumably, you want to link to script.js?c=21 and internally rewrite this to script.js.php?c=21 (the underlying file), thus hiding the .php file extension from users. In which case, try something like the following instead:
server {
location ~ \.js$ {
rewrite (.*) $1.php last;
}
}
To answer your specific queries:
- Is that possible?
Yes, but the "reverse" is probably what you really want to be doing. (?)
- Is that a good method having such a rewrite?
It's OK. Presumably, you have a requirement to mix PHP and JavaScript?
- Is wrong having query string
?c=21on file location (to populate from PHP file with givencattribute)?
I don't think there is necessarily anything wrong with this. Bear in mind that some caching proxies may ignore the query string, but I think that is mostly historic.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
