使用 Negative Lookahead 的 RewriteRule 导致服务器错误

RewriteRule using Negative Lookahead results in Server Error

我有以下使用 Negative Lookahead 的 htaccess 规则

RewriteRule ^(.*)\/(?!.*(contact|about|subscribe)).*$ //?section=other

预期的 url 结果是 domain.com/dir/?section=other 当请求不是 dir/contact 等时。 当文件放在服务器上时,结果是 Internal Server Error.

Here it at regex101

如有任何帮助,我们将不胜感激。这件事我跟周日的校车一样一头雾水

根据您显示的尝试,请尝试遵循 htaccess 规则文件。您可以检查其 uri 部分以确保其中不存在不需要的部分,然后我们可以对 uri 执行重写。请确保在测试您的 URL 之前清除您的浏览器缓存。还要确保您的 htaccess 文件存在于您的根文件夹中。

RewriteEngine ON
RewriteCond %{REQUEST_URI} !(contact|about|subscribe) [NC]
RewriteRule ^(.*)/?$ //?section=other [QSA,L]

When the file is placed on the server, the result is Internal Server Error

您收到 500(内部服务器错误),因为您的规则 运行 进入循环,因为正则表达式模式不断匹配重写 URL,也就是 /dir/

您可以使用此规则来解决此问题:

RewriteRule ^([^/]+)/(?!.*(contact|about|subscribe|favorite)).+$ /?section=other [QSA,L,NC]

请注意在 / 之后使用 .+ 以防止它只匹配 /dir/

Updated RegEx Demo