简体   繁体   中英

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

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:
this works, but for both url's

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

this should be a match with an online regEx tester, but does not 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;

}

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 "^/\d{1,4}(/.*)$" $1 redirect;

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

See this document for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM