如何编写以下规则来针对特定类型的文件进行自定义?
看,规则是:
RewriteCond %{REQUEST_FILENAME} !-f
当我将此规则放入 .htaccess 中时,我告诉网络服务器:忽略 URL 中的所有文件路径。
例如:http://www.foo.com/images/file.jpg
如果此 URL 由脚本 (.php) 处理并返回文件的内容,则该文件将被忽略,仅显示图像。我需要这样做,但不是所有文件:
例如:.JS 文件 NOT 和其他文件类型。
如何“重写”该规则以忽略特定类型的文件或仅适用于特定文件,例如:
仅接受:JPG、PNG、JPEG、MP3
RewriteCond %{REQUEST_FILENAME}.(jpg|png|jpeg|mp3 ... ) !-f
或
不接受:JS、TXT、CSS
RewriteCond !%{REQUEST_FILENAME}.(js|txt|css| ... ) !-f
Look, the rule is:
RewriteCond %{REQUEST_FILENAME} !-f
Whe I put this rule in the .htaccess I'm telling to the webserver that: IGNORE ALL THE FILE PATHS IN THE URL.
For example: http://www.foo.com/images/file.jpg
This file is ignored, the image will only showed, if this URL is processed by a script (.php) and returns the content of the file. I need to do this, but not with all the files:
For example: .JS files NOT and other file types.
How to "REWRITE" that rule to ignore specific type of files or to apply only for specific files like:
ONLY ACCEPTS: JPG, PNG, JPEG, MP3
RewriteCond %{REQUEST_FILENAME}.(jpg|png|jpeg|mp3 ... ) !-f
OR
NO ACCEPTS: JS, TXT, CSS
RewriteCond !%{REQUEST_FILENAME}.(js|txt|css| ... ) !-f
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
忽略 URL 中的所有文件路径。
您要求检查所请求的文件是否存在。 !-f 表示“请求 URL 指向的物理位置不存在文件”。因此,如果该文件确实存在于请求的位置,则您的 php 脚本将不会被执行。目前尚不清楚为什么要添加一些排除,因为如果文件存在,无论如何都会请求它,如果不存在,您的脚本应该生成带有 404 错误的页面,否则它将是服务器生成的页面。可能不是那么花哨:)
除了第一个答案之外,它可能应该是
为了摆脱区分大小写。
组合在一起
IGNORE ALL THE FILE PATHS IN THE URL.
You are telling to check the existence of the requested file. !-f means "file does not exist at the physical location where the request URL is pointing". So, if the file actually exists at requested location, you php script will not be executed. That is not clear why do you want to add some exclusion, because if the file exists, it will be requested anyway and if it does not exists, your script should generate page with 404 error, otherwise it will be the page generated by the server. Probably not so fancy :)
In addition to the first answer, it should be, probably,
in order to get rid of case sensitivity.
Combining together
如果 url 以列出的扩展名之一结尾,则匹配:
如果 url 不以列出的扩展名之一结尾,则匹配:
默认情况下,条件通过“AND”连接。仅当 A 和 B 匹配时才会匹配:
如果 A 或 B 匹配,则会匹配:
更多详细信息: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond
Will match if the url ends with one of listed extensions:
Will match if the url doesn't end with one of listed extensions:
The conditions by default are connected by "AND". This will match only if both A and B match:
This will match if A or B match:
More details: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond