在 nginx 上重定向 AMP 文件

Redirect AMP files on nginx

我正在尝试在 nginx 上进行重定向,但不幸的是它不起作用。 我想实现的是在移动设备上重定向 amp 文件。

我想做什么:

来自

https://www.example.com/uri-759.html

https://www.example.com/uri-759-amp.html

我做的重定向

if ($mobile_redirect = perform) {

    redirect ^(.*)(\.html)$ -amp permanent;
}

我得到了什么

https://www.example.com/uri-759-amp-amp-amp-amp-amp-amp-amp-amp.html

有人有执行此重定向的解决方案吗?

您可以使用否定后向断言来避免匹配重写的 URI。

例如:

rewrite ^(.*)(?<!-amp)(\.html)$ -amp permanent;

有关更多信息,请参阅 this document