用 nginx 重写 404 url
Rewrite with 404 urls with nginx
我正在尝试重写一些显示为 404 的网址,但我无法让重写工作。网址看起来像这样 /ossobuco-alla-milanese/1451114854360.1451114854360?time=1451114851111
。我想通过重写删除 1451114854360.1451114854360?time=1451114851111
。
在我的 nginx 配置中,我有以下重写规则
rewrite "^\/(.*)\/(\d{13}\.\d{13}\?time=\d{13})$" // permanent;
我在 2 个在线正则表达式工具中测试了正则表达式 regex101 and regex pal,它应该可以工作,但在我的服务器上似乎不起作用。
要匹配查询字符串,请使用 $args
:
location / {
if ($args ~* "^time=\d+") {
set $args '';
rewrite "^/(.+)/\d+\.\d+/?$" / permanent;
}
}
PS:如果你只想匹配13digits.13digits
那么使用:
rewrite "^/(.+)/\d{13}\.\d{13}/?$" / permanent;
最后它只是一个非常简单的位置块和?删除参数。
location ~* ^/(.+)/\d+\.\d+$ {
rewrite ^/(.+)/\d+\.\d+$ /? permanent;
}
我正在尝试重写一些显示为 404 的网址,但我无法让重写工作。网址看起来像这样 /ossobuco-alla-milanese/1451114854360.1451114854360?time=1451114851111
。我想通过重写删除 1451114854360.1451114854360?time=1451114851111
。
在我的 nginx 配置中,我有以下重写规则
rewrite "^\/(.*)\/(\d{13}\.\d{13}\?time=\d{13})$" // permanent;
我在 2 个在线正则表达式工具中测试了正则表达式 regex101 and regex pal,它应该可以工作,但在我的服务器上似乎不起作用。
要匹配查询字符串,请使用 $args
:
location / {
if ($args ~* "^time=\d+") {
set $args '';
rewrite "^/(.+)/\d+\.\d+/?$" / permanent;
}
}
PS:如果你只想匹配13digits.13digits
那么使用:
rewrite "^/(.+)/\d{13}\.\d{13}/?$" / permanent;
最后它只是一个非常简单的位置块和?删除参数。
location ~* ^/(.+)/\d+\.\d+$ {
rewrite ^/(.+)/\d+\.\d+$ /? permanent;
}