【发布时间】:2020-04-24 03:56:52
【问题描述】:
我正在尝试从 WordPress 迁移到自托管的 Ghost 博客。在此过程中,我希望清理从https://example.com/categoryid/slug 到https://example.com/slug 的帖子的网址。 Cateogryid 似乎是包含 1-4 位数字的整数。
问题是我也有不想重写的网址
- 不要为 img 重写:
https://example.com/content/images/2020/01/logo.png - 为帖子重写:
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