如何在Nginx中将WordPress的URL重写从/id/slug改写为Ghost中的slug?

huangapple go评论79阅读模式
英文:

How to rewrite url /id/slug (WordPress) to just slug (Ghost) in Nginx?

问题

I'm trying to migrate from WordPress to a self-hosted Ghost blog. In the process, I wish to clean up URLs for my posts from https://example.com/categoryid/slug to https://example.com/slug. Categoryid is a whole number containing 1-4 digits.

The problem is that I have other URLs that I don't want to rewrite:

  1. Don't rewrite for img: https://example.com/content/images/2020/01/logo.png
  2. Rewrite for post: https://example.com/1886/slug

What I have tried:

This works, but for both URLs:

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

This should match with an online regex tester, but it doesn't work:

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

Passing proxy before and after rewrite rules:

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 ;
}

Full config:

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;
}
英文:

I'm trying to migrate from WordPress to self hosted Ghost blog. In the process I wish to clean up url's for my posts from https://example.com/categoryid/slug to https://example.com/slug. Cateogryid is seems to be whole number containing 1-4 digits.

The problem is that I have also urls that I don't want to rewrite

  1. don't rewrite for img: https://example.com/content/images/2020/01/logo.png
  2. rewrite for post: https://example.com/1886/slug

What I have tried:<br />
this works, but for both url's<br />

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

this should be a match with an online regEx tester, but does not work<br />

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

passing proxy before and after rewrite rules<br />

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 ;
}

full config:

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 -&gt; slug
    rewrite ^(.*)(\/\d*\/)(.*)$ https://example.com/$3 redirect;
    # redirect category -&gt; tag
    rewrite (category\/)(.*)$ https://example.com/tag/$2 permanent;
    # redirect blog -&gt; 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;

}

答案1

得分: 1

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

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

可以使用以下之一:

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

或者:

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

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

有关详细信息,请参阅此文档

英文:

In the URLs https://example.com/content/images/2020/01/logo.png and https://example.com/1886/slug, the URI seen by the rewrite directive is /content/images/2020/01/logo.png and /1886/slug respectively.

You need to rewrite URIs which contain 1 to 4 digits in the first path element.

Use either:

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

Or:

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

The last variant must use the quotes to protect the embedded brace characters.

See this document for details.

huangapple
  • 本文由 发表于 2020年1月6日 23:40:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614942.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定