.htaccess 如果文件存在重写规则
我发现了几个类似的问题,但似乎我需要你的帮助,因为我无法自己写它。
我想要这个: http://domain.com/folder/file.123.ext (没有 123 也应该工作)
被重写为 /folder/file.min.ext (如果存在)
或 /folder/file.ext (如果缩小版本不存在)
它用于我的 css 和 js 文件..所以它会加载对于那些没有压缩版本的文件。
我目前有这个:
RewriteRule ^(css|js)/([a-zA-Z_-]+)\.([0-9]+)\.(css|js)$ /$1/$2.min.$4
RewriteRule ^(css|js)/([a-zA-Z_-]+)\.(css|js)$ /$1/$2.min.$3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(css|js)/([a-zA-Z_-]+)\.min\.(css|js)$ /$1/$2.$3
当我删除第二行时它工作正常......但我需要它在没有数字的情况下也能工作:(
I found a couple of similar questions, but it seems like i need your help, because i'm not able to write it on my own.
I want this: http://domain.com/folder/file.123.ext (should work also without 123)
to be rewritten to /folder/file.min.ext (if it exists)
or /folder/file.ext (if the minified version doesn't exist)
It's for my css and js files ... so it will load non-minified versions for those files that doesn't have ones.
I currently have this:
RewriteRule ^(css|js)/([a-zA-Z_-]+)\.([0-9]+)\.(css|js)$ /$1/$2.min.$4
RewriteRule ^(css|js)/([a-zA-Z_-]+)\.(css|js)$ /$1/$2.min.$3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(css|js)/([a-zA-Z_-]+)\.min\.(css|js)$ /$1/$2.$3
it works fine when i remove the second line ... but i need it to work also without the number :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能用这个替换 2 条规则吗?
Can't you just replace the 2 rules with this?
根据 Jon 的回答,这应该可以解决重定向循环问题:
第一行将阻止在重定向时执行附加规则。
如果您发现任何其他问题或需要进一步调整,请尝试使用 RewriteLog 和 RewriteLogLevel(级别 5)来调试规则集并准确找出 mod_rewrite 正在做什么。
Following on from Jon's answer, this should solve the redirect loop problem:
The first line will prevent the attached rule from being executed on redirect.
If you find any other issues or need to tweak this further, try using RewriteLog and RewriteLogLevel (at level 5) to debug the rule set and find out exactly what mod_rewrite is doing.