如何为request_filename写入不同于指定的重写条件


How to write rewrite condition for request_filename different then specified

我面临htaccess的问题,我想写一个条件来阻止为指定的文件提供服务。

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

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]

但是我怎样才能知道一个指定的文件不会被提供?我将此路径用于控制器操作。例如CCD_ 2是文件do_something.php的路径,但也是我的控制器操作的路径,我希望我的htaccess文件提供此操作,而不是此文件,但仅此。

正如anubhava在回答时所说,我试过了

RewriteRule ^do_something'.php$ - [L,NC]

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

RewriteRule ^example/(.+)'.php$ %{ENV:BASE}/app.php [L]

因此,在这种情况下,如果有人输入路径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]

您可以使用以下规则:

RewriteRule ^example/do_something'.php$ - [L,NC]

这将跳过此规则下的所有规则中的do_something.php