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



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

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

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

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

尝试用实现这一点

rewrite ^(/.*).html(?.*)?$ $1$2 permanent;

并添加一个负面的后备:

rewrite ^(?!plugins)(/.*).html(?.*)?$ $1$2 permanent;

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

这应该有效:

rewrite ^(?!/[^/]+/plugins/)(/.*).html(?.*)?$ $1$2 permanent;

测试:

/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之前的任何内容,并捕获之后的任何内容(如果有的话(

最新更新