如何将除现有文件之外的所有请求映射到处理程序

How to map all requests except existing files to a handler

需要一些关于 htaccess 重写规则的帮助。

我有一个处理程序 index.html 用于处理除现有文件之外的所有请求。 我正在使用这条规则:

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

但我有一些文件名与要处理的请求相似。如果有对 /main 的请求,它必须由 index.html 处理,但由于现有文件的扩展名 - main.asdfjaskldfjaskdfjasfd.js - 我收到 404 错误而不是处理。

这个问题在 nginx 中非常容易解决,只需一个 liner:"try_files $uri $uri/ /index.html;" 但我没有机会在生产中使用 nginx - 只有 Apache。

所以我需要一些关于这些规则的帮助。 试过(除了其他几十个)这个:

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteRule ^ /index.html

完整的 htaccess:

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ / [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_URI} ^/(demo-auth|api|oauth|password|login|logout|register|images)
RewriteRule ^ laravel.php [L]

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^ /index.html

RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

提前致谢。

在您的 htaccess 顶部添加,以禁用 MultiViews:

Options -MultiViews

Apache docs on mod_negotiation 描述了多视图选项在启用时的作用:

If the server receives a request for /some/dir/foo and /some/dir/foo does not exist, then the server reads the directory looking for all files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements, and returns that document.