Regex nginx重写删除html,除非URL包含字符串

Regex nginx重写删除html,除非URL包含字符串,regex,nginx,url-rewriting,Regex,Nginx,Url Rewriting,我们需要将所有包含html的链接重定向到非html,例如: domain.com/post.html->domain.com/post domain.com/2010/10/post.html->domain.com/2010/10/post 但是,我们需要排除路径中包含“插件”的URL,例如: domain.com/wp-content/plugins/something/test.html不应重定向 试图通过以下方式实现这一目标: rewrite^(/.*)\.html(\?.*)$$1$2

我们需要将所有包含html的链接重定向到非html,例如:

domain.com/post.html->domain.com/post domain.com/2010/10/post.html->domain.com/2010/10/post

但是,我们需要排除路径中包含“插件”的URL,例如:

domain.com/wp-content/plugins/something/test.html不应重定向

试图通过以下方式实现这一目标:

rewrite^(/.*)\.html(\?.*)$$1$2永久

并添加了一个负面回顾:

rewrite^(?!plugins)(/.*)\.html(\?.*)$$1$2永久

我尝试的任何变化似乎都有问题。或者仍然会从URL中删除.html,即使URL中包含插件。

这应该可以:

rewrite^(?!/[^/]+/plugins/)(/.*)\.html(\?.*)$$1$2永久性

测试:

/post.html         ==> domain.com/post
/post.html?foo=bar ==> domain.com/post?foo=bar
/2010/10/post.html ==> domain.com/2010/10/post
/wp-content/plugins/something/test.html (no match)
正则表达式的解释:

  • ^
    <代码>$
-在开头和结尾锚定
  • (?!/[^/]+/plugins/)
    -在第二个子目录中应为负前瞻
    /plugins/
  • (/.*)\.html(\?*)$
    -捕获
    .html
    之前的任何内容,并捕获之后的任何内容(如果有)

  • 正是需要的,也感谢您的解释