我遇到了 htaccess
的问题,我想编写一个条件来阻止提供指定文件。
如果请求的文件名存在,只需提供它,这样我就有了:
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
但是我怎样才能做到不提供一个指定的文件呢?我有这条路径用于我的 Controller 操作。所以例如 example/do_something.php
是文件 do_something.php
的路径,但也是我的 Controller 操作的路径,我希望我的 htaccess
文件服务于此操作而不是此文件,但仅此。
最佳答案
RewriteRule ^do_something\.php$ - [L,NC]
它对我不起作用,但我设法通过将这些文件重定向到我的前端 Controller 来解决它:
RewriteRule ^example/(.+)\.php$ %{ENV:BASE}/app.php [L]
所以在这种情况下,如果有人输入路径 example/do_something.php
,文件将不会被提供并且请求由前端 Controller 发送
完整示例:
# 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]
关于php - 如何为不同于指定的 request_filename 编写重写条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29513358/