【问题标题】:How to rewrite url /id/slug (WordPress) to just slug (Ghost) in Nginx?如何在 Nginx 中将 url /id/slug (WordPress) 重写为 slug (Ghost)?
【发布时间】:2020-04-24 03:56:52
【问题描述】:

我正在尝试从 WordPress 迁移到自托管的 Ghost 博客。在此过程中,我希望清理从https://example.com/categoryid/slughttps://example.com/slug 的帖子的网址。 Cateogryid 似乎是包含 1-4 位数字的整数。

问题是我也有不想重写的网址

  1. 不要为 img 重写:https://example.com/content/images/2020/01/logo.png
  2. 为帖子重写:https://example.com/1886/slug

我尝试过的:
这有效,但对于两个网址的

rewrite ^(.*)(\/\d*\/)(.*)$ https://example.com/$3 redirect;

这应该与在线正则表达式测试器匹配,但不起作用

rewrite \.*(com)(\/\d*\/)(.*)$ https://example.com/$3 redirect;

在重写规则之前和之后传递代理

location /content/ {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
    proxy_pass example_ip ;
}

完整配置:

server {
    listen 80;
    listen [::]:80;
    server_name         www.example.com example.com;

    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;


    # redirect /id/slug -> slug
    rewrite ^(.*)(\/\d*\/)(.*)$ https://example.com/$3 redirect;
    # redirect category -> tag
    rewrite (category\/)(.*)$ https://example.com/tag/$2 permanent;
    # redirect blog -> archive
    rewrite (blog\/)$ https://example.com/archive permanent;

    root /var/www/ghost/system/nginx-root; # Used for acme.sh SSL verification (https://acme.sh)
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/nginx/snippets/ssl-params.conf;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass http://example_ip;
    }

    location ~ /.well-known {
        allow all;
    }

    client_max_body_size 50m;

}

【问题讨论】:

    标签: wordpress nginx url-rewriting ghost-blog


    【解决方案1】:

    https://example.com/content/images/2020/01/logo.pnghttps://example.com/1886/slug 的URL 中,rewrite 指令看到的URI 分别是/content/images/2020/01/logo.png/1886/slug

    您需要重写在第一个路径元素中包含 1 到 4 位数字的 URI。

    使用任一:

    rewrite ^/\d+(/.*)$ $1 redirect;
    

    或者:

    rewrite "^/\d{1,4}(/.*)$" $1 redirect;
    

    最后一个变体必须使用引号来保护嵌入的大括号字符。

    详情请见this document

    【讨论】:

    • 完全错过了 rewrite 实际看到的部分。我接受了第一个建议,但必须进行一些调整,因为它在域之后留下双斜杠 (//) 而不是单斜杠。这个似乎可以解决问题:永久重写 ^\/\d+\/(.*)$ $1 。 @RichardSmith 那么现在呢?你会编辑答案然后我接受吗? (有史以来第一个问题)
    • 或者更准确地说,我是这样使用的:rewrite ^\/\d+\/(.*)$ https://example.com/$1 permanent;
    • 您可以根据需要指定方案和主机,但注意不要引入额外的/。但是,带有redirectpermanent 后缀的方案和主机不是必需的。
    猜你喜欢
    • 2020-08-04
    • 2013-09-04
    • 2021-03-19
    • 2015-05-21
    • 2019-08-01
    • 2011-11-20
    • 2021-09-17
    • 2020-10-22
    • 1970-01-01
    相关资源
    最近更新 更多