Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess 如何在htaccess中执行:如果文件夹中的文件存在,则为其提供服务,如果不存在,则提供blank.html_.htaccess_Mod Rewrite - Fatal编程技术网

.htaccess 如何在htaccess中执行:如果文件夹中的文件存在,则为其提供服务,如果不存在,则提供blank.html

.htaccess 如何在htaccess中执行:如果文件夹中的文件存在,则为其提供服务,如果不存在,则提供blank.html,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我有一个htaccess文件,它将所有请求重定向到index.php,以便CMS系统能够解析请求并提供答案。但是,由于明显的原因,对于请求路径上实际存在的文件(在下面的情况下,这些文件是用户使用fck编辑器上载的文件),这是不合适的,因此我在htaccess中有如下规则之一: RewriteRule ^(.*)/fck/(.*) - [L] RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{DOCUMENT_ROOT}%{REQUEST_

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

RewriteRule ^(.*)/fck/(.*) - [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f
RewriteRule ^.*$ - [L]
这很有效,但问题是,现在我需要创建一个相反的规则:即当文件夹
/fck/
中的文件不存在时,返回一些空白页面(或者找不到,或者我们想怎么称呼它),而不必为CMS操心


这应该很容易在htaccess中完成(好吧,好吧,当不得不问这个问题时听起来很傻,但我根本不是htaccess的专家…)。有人能告诉我怎么做吗?

这应该能帮到你:

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文件夹是子文件夹(例如
example.com/one/fck/icon.png
)。如果fck文件夹位于网站根目录中(即
example.com/fck/icon.png
),则需要将模式更改为
^fck/*
,因为.htaccess中的URL在重写规则中进行匹配时没有前导斜杠
/
(除非这些规则在服务器/虚拟主机上下文中声明,而不是.htaccess)

2) 我不会说我100%肯定这一点(仅99.99%),但这两行是相同的:

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

这将为您完成以下工作:

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文件夹是子文件夹(例如
example.com/one/fck/icon.png
)。如果fck文件夹位于网站根目录中(即
example.com/fck/icon.png
),则需要将模式更改为
^fck/*
,因为.htaccess中的URL在重写规则中进行匹配时没有前导斜杠
/
(除非这些规则在服务器/虚拟主机上下文中声明,而不是.htaccess)

2) 我不会说我100%肯定这一点(仅99.99%),但这两行是相同的:

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f
这对我很有用:

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (.*) $1 [QSA,L]
这对我很有用:

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (.*) $1 [QSA,L]