如何为request_filename写入重写条件,然后指定

var*_*ius 5 php .htaccess

我遇到了问题htaccess,我想编写一个条件,阻止提供指定的文件.

如果请求的文件名存在,只需提供它,所以我有这个:

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
Run Code Online (Sandbox Code Playgroud)

但我怎么能得到一个指定的文件不会被提供?我有这条路径用于我的控制器动作.所以例如example/do_something.php 是文件的路径,do_something.php但也是我的控制器操作的路径,我希望我的htaccess文件服务此操作而不是此文件,但只有这个.

var*_*ius 0

我尝试了阿努巴瓦在他的回答中所说的

RewriteRule ^do_something\.php$ - [L,NC]
Run Code Online (Sandbox Code Playgroud)

它对我不起作用,但我设法通过将此文件重定向到我的前端控制器来解决它:

RewriteRule ^example/(.+)\.php$ %{ENV:BASE}/app.php [L]
Run Code Online (Sandbox Code Playgroud)

因此,在这种情况下,如果有人输入路径,example/do_something.php则不会提供文件,并且请求将由前端控制器发送

完整示例:

# If the requested filename exists
# And my route path for controller action is the same
# Don't serve file serve action
RewriteRule ^example/(.+)\.php$ %{ENV:BASE}/app.php [L]

# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]

# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/app.php [L]
Run Code Online (Sandbox Code Playgroud)