php - 如何为不同于指定的 request_filename 编写重写条件

标签 php .htaccess

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

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

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

但是我怎样才能做到不提供一个指定的文件呢?我有这条路径用于我的 Controller 操作。所以例如 example/do_something.php 是文件 do_something.php 的路径,但也是我的 Controller 操作的路径,我希望我的 htaccess 文件服务于此操作而不是此文件,但仅此。

最佳答案

我试过 anubhava在他的answer中说

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/

相关文章:

php - PHP类中的函数文字

php - 你如何处理 PHP 中的时差计算?

php - 如何处理 .htaccess 规则中的 & 和/等特殊字符?

apache - 如何在 htaccess 中删除目录上的斜线?

.htaccess - 将域更改为静态 IP,SEO Joomla 友好 URL 不起作用

php - 我想从 mysql 数据库中获取复选框值

php - yii2 将 css 类动态应用到复选框选项

php - 使用 mysql_num_rows(),如何判断 myql_query() 返回的结果是 0 还是 php 中的 sql 错误

.htaccess - htaccess 重写会破坏相对路径

javascript - 具有双重操作的页面重定向