【问题标题】:Rewrite nginx rule for delete ID on url重写 nginx 规则以删除 url 上的 ID
【发布时间】:2017-01-09 19:29:34
【问题描述】:

我需要在 url post (wordpress) 上写一个删除 id 的规则

https://myqite.com/12-title-of-post-one
https://myqite.com/45-title-of-post-two
https://myqite.com/56-title-of-post-nn

https://myqite.com/title-of-post-one
https://myqite.com/title-of-post-two
https://myqite.com/title-of-post-nn

尝试了几个选项,但都失败了。

rewrite ^([0-9]+)-(.+)$ $2 permanent;

其他

rewrite ^([0-9]+)-(.+)$ $2 last;

【问题讨论】:

  • 正则表达式 ([0-9]+) 匹配所有数字。将rewrite ^([0-9]+)-(.+)$ $2 last; 替换为rewrite ^(.+)$ $2 last; 会得到什么?
  • 正则表达式完美运行。 ([0-9]+- 匹配 url 的数量,例如 59-。这个元素不喜欢。它是第一个 var o 正则表达式。(.*) 它是 var 的第二部分。这是一个类似的 url。在帖子上,它的解释非常干净我喜欢什么。谢谢。rubular.com/r/pp4pjDZAac
  • nginx URI 以 / 开头。您可能应该使用类似:rewrite ^/[0-9]+-(.+)$ /$1
  • rewrite ^/[0-9]+-(.+)$ /$1 last;不起作用。这条线在其他之后工作正常。 rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;谢谢

标签: wordpress nginx url-rewriting


【解决方案1】:

经过多次测试,我得到了解决方案

location ~ /([0-9]+)-(.+) {
   return 301 https://$server_name/$2;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    • 2017-03-13
    相关资源
    最近更新 更多