htaccess redirect 匹配除特定类型之外的所有文件

htaccess redirectMatch all files of a type except a certain one

假设我有一个网络文件夹:

/index.html
/assets/css/styles.css
/assets/images/logo.png
/something.html
/holdingpage.html
/finale.html
/folder/somefile.html
/else.html
/PDFs/something.pdf

我想为此设置一个 htaccess RedirectMatch 以“捕获”除 finale.html 之外的所有 html 文件并重定向到那里。

我目前拥有的是:

RedirectMatch 302 ".*/$" "/finale.html"

正确捕获所有没有文件名(索引)的文件夹。 我需要的是捕获所有 html 但不是那个目的地。

RedirectMatch 302 ".*(\.html)$" "/finale.html" # This loops obviously. 

所以我试着说得更具体一些;

RedirectMatch 302 ".*!(finale.html)$" "/finale.html" 

RedirectMatch 302 ".*!(finale\.html)$" "/finale.html" 

但这不会捕获其他文件。另外从它的外观来看,我希望它也能捕获资产和图像。

我正在尝试找到一个 redirectMatch,它可以找到 NOT finale.html 的每个 .html 页面并将其重定向到该页面。

IT 应该是 redirectMatch 而 NOTrewriteCond / rewriteRule。用户需要看到他们正在被重定向。

我想我想要这样的东西:

RedirectMatch 302 ".*((\.html) BUT NOT (finale.html))$" "/finale.html" 

我已经阅读了Apache Documentation,但这并没有详细说明这个概念。

您可以使用否定前瞻来匹配除 /finale.html(在文档根目录中)之外的所有 .html 文件。

例如:

RedirectMatch 302 (?!^/finale\.html$)^/.+\.html$ /finale.html

IT should be a redirectMatch and NOT a rewriteCond / rewriteRule. The user needs to see they're being redirected.

我不确定这里的逻辑。 mod_rewrite 可以产生与 RedirectMatch 相同的外部重定向,事实上,如果您有现有的 mod_rewrite 指令,您应该在这里使用 mod_rewrite (主要是为了避免意外冲突) .

使用 mod_rewrite,您可以使用与上面类似的负面前瞻来执行此操作。例如:

RewriteEngine On

RewriteRule (?!^finale\.html$)^.+\.html$ /finale.html [R=302,L]

或者,使用额外的 条件 finale.html 设置例外,而不是使用否定前瞻。例如:

RewriteCond %{REQUEST_URI} !^/finale\.html$
RewriteRule \.html$ /finale.html [R=302,L]

这可以说更容易阅读并且可能更有效(至少是正则表达式)。

这匹配所有以 .html 结尾的 URL,除了 /finale.html(完全)和外部 302 重定向到 /finale.html.


旁白:

RedirectMatch 302 ".*!(finale.html)$" "/finale.html" 

正则表达式中间的!在这里是一个文字字符。 !prefix-operator是mod_rewrite的一个特征。 (它是 prefix-operator,不是正则表达式语法本身的一部分。