在NGINX中重写错误的URL到.html

前一段时间,我写了一个错误,Google把它们caching在旧的站点地图中。 这在WMT中产生了大量的404错误,我需要用一个可靠的重写进行清理。 我目前使用地图模块,并单独重写每个产品。

例:

/product1html/ /product1.html; 

在这里粗体的错误: http:// domain / productname html /

这里的错误是我最初忘了。 在html之前,并在最后有一个/。

我怎样才能重写url为http://domain/productname.html每个url看起来像/productnamehtml/

重写将如下所示:

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

或者你可能想把它放到一个位置:

 location ~* ^(?<product>.*[^.])html/?$ { return 301 $product.html; } 

解释:两个正则expression式是相同的,除了后者有一个命名的捕获product 。 我们寻找任何以非点结尾的顺序,然后是html (可选) / 。 捕获html之前的所有内容。