当页面中有相关文件时运行 Slim Framework 应用程序

Slim Framework app is runned when a relative file is in the page

当请求包含具有相对路径的文件/图像的页面时,应用程序将为每个请求 运行。所以应用程序被引导并执行了多次。

当图像文件名中有下划线时似乎会出现问题

<img src="assets/images/_download.jpg" alt="test">
<img src="assets/images/_downlaod2.jpg" alt="test">
<img src="assets/images/_downlod3.jpg" alt="test">

我注意到这一点是因为我正在使用 Xdebug。我想知道如何预防这种情况以及最佳做法是什么?

.htaccess

<IfModule mod_rewrite.c>
  RewriteEngine On

  # Some hosts may require you to use the `RewriteBase` directive.
  # Determine the RewriteBase automatically and set it as environment variable.
  # If you are using Apache aliases to do mass virtual hosting or installed the
  # project in a subdirectory, the base path will be prepended to allow proper
  # resolution of the index.php file and to redirect to the correct URI. It will
  # work in environments without path prefix as well, providing a safe, one-size
  # fits all solution. But as you do not need it in this case, you can comment
  # the following 2 lines to eliminate the overhead.
  RewriteCond %{REQUEST_URI}:: ^(/.+)/(.*)::$
  RewriteRule ^(.*) - [E=BASE:%1]

  # If the above doesn't work you might need to set the `RewriteBase` directive manually, it should be the
  # absolute physical path to the directory that contains this htaccess file.
  # RewriteBase /

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^ index.php [QSA,L]
</IfModule>

您缺少文件夹条件:

RewriteCond %{REQUEST_FILENAME} !-d

如果请求解析为文件夹,它会阻止重写。因此,根据您的 htaccess 代码,所有请求都被重写到 index.php 文件,但解析为文件的请求除外。由于您的图像位于一个文件夹中,因此它们首先解析为一个文件夹。

您的重写应该如下所示:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]