htaccess中的操作方法:如果文件夹中的文件存在,则提供该文件;如果不存在,则提供blank.html [英] How to do in htaccess: if file in folder exists, serve it, if not, provide blank.html

查看:67
本文介绍了htaccess中的操作方法:如果文件夹中的文件存在,则提供该文件;如果不存在,则提供blank.html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有htaccess文件,它将所有请求重定向到index.php,以便CMS系统可以解析请求并提供答案.但是出于明显的原因,这对于请求路径中实际存在的文件(在下面的情况下是用户使用fck编辑器上传的文件)来说并不可行,因此我在htaccess中有如下规则之一:

I have htaccess file that redirects all requests to index.php, so that CMS system can parse the request and provide answer. However for obvious reasons this is not OK for files that actually exist on the requested path (in below case these are files uploaded by users using fck editor), therefore I have one of the rules in htaccess as follows:

RewriteRule ^(.*)/fck/(.*) - [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f
RewriteRule ^.*$ - [L]

这很好用,但是问题是,现在我需要创建相反的规则:即,当文件夹/fck/中的文件不存在时,返回一些空白(或找不到或我们要调用它)页,并且不要为此而烦扰CMS.

This works great, but the problem is, that now I need to create the opposite rule: i.e. when file from folder /fck/ does NOT exist, then return some blank (or not found or however we want to call it) page and do not bother the CMS with that.

在htaccess中完成此操作应该很容易(嗯,好吧,当不得不问这个问题时,听起来很傻,但是我对htaccess一点都不熟练.)有人可以让我知道怎么做吗?

This should be easily (well, OK, easily sounds silly when have to ask this question, but I am not expert to htaccess at all..) done in htaccess. Could someone please let me know how to do that?

推荐答案

这应该为您完成工作:

RewriteEngine On
RewriteBase /

# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]

# return /blank.php if no such file or folder in /fck/
RewriteCond ${REQUEST_FILENAME} !-f
RewriteCond ${REQUEST_FILENAME} !-d
RewriteRule ^.*/fck/.* /blank.php [L]
# or use below line instead of the above line (comment and uncomment accordingly)
# it will display 404 page (using 404 handler from your config: e.g. ErrorDocument 404 /404.php)
#RewriteRule ^.*/fck/.* - [R=404,L]

# your CMS rewrite rules go below

注释:

1)此模式^.*/fck/.*假定 fck 文件夹是子文件夹(例如example.com/one/fck/icon.png).如果 fck 文件夹位于网站根目录(即example.com/fck/icon.png)中,则需要将模式更改为^fck/.*,因为.htaccess中的URL在RewriteRule中匹配时没有前导斜杠/(除非这些规则在服务器/虚拟主机上下文中声明,而不是.htaccess).

1) This pattern ^.*/fck/.* assumes that fck folder is a subfolder (e.g. example.com/one/fck/icon.png). If fck folder is located in website root (i.e. example.com/fck/icon.png) then pattern need to be changed to ^fck/.* as URLs in .htaccess have no leading slash / when matching in RewriteRule (unless these rules are declared in server/virtual host context and not .htaccess).

2)我不会说我对此有100%的把握(只有99.99%),但是这两行是相同的:

2) I will not say that I'm 100% sure on this (only 99.99%), but these 2 lines are the same:

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f

这篇关于htaccess中的操作方法:如果文件夹中的文件存在,则提供该文件;如果不存在,则提供blank.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆