如何在 Nginx 中将 url /id/slug (WordPress) 重写为 slug (Ghost)?
How to rewrite url /id/slug (WordPress) to just slug (Ghost) in Nginx?
我正在尝试从 WordPress 迁移到自托管的 Ghost 博客。在此过程中,我希望将我的 post 的 url 从 https://example.com/categoryid/slug
清理到 https://example.com/slug
。 Cateogryid 似乎是包含 1-4 位数字的整数。
问题是我还有 url 不想重写
- 不要为 img 重写:
https://example.com/content/images/2020/01/logo.png
- 为 post 重写:
https://example.com/1886/slug
我试过的:
这有效,但对于 url 的
rewrite ^(.*)(\/\d*\/)(.*)$ https://example.com/ redirect;
这应该与在线正则表达式测试器相匹配,但不起作用
rewrite \.*(com)(\/\d*\/)(.*)$ https://example.com/ 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/ redirect;
# redirect category -> tag
rewrite (category\/)(.*)$ https://example.com/tag/ 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;
}
在 URL https://example.com/content/images/2020/01/logo.png
和 https://example.com/1886/slug
中,rewrite
指令看到的 URI 分别是 /content/images/2020/01/logo.png
和 /1886/slug
。
您需要重写第一个路径元素中包含 1 到 4 位数字的 URI。
使用其中之一:
rewrite ^/\d+(/.*)$ redirect;
或:
rewrite "^/\d{1,4}(/.*)$" redirect;
最后一个变体必须使用引号来保护嵌入的大括号字符。
详情见this document。
我正在尝试从 WordPress 迁移到自托管的 Ghost 博客。在此过程中,我希望将我的 post 的 url 从 https://example.com/categoryid/slug
清理到 https://example.com/slug
。 Cateogryid 似乎是包含 1-4 位数字的整数。
问题是我还有 url 不想重写
- 不要为 img 重写:
https://example.com/content/images/2020/01/logo.png
- 为 post 重写:
https://example.com/1886/slug
我试过的:
这有效,但对于 url 的
rewrite ^(.*)(\/\d*\/)(.*)$ https://example.com/ redirect;
这应该与在线正则表达式测试器相匹配,但不起作用
rewrite \.*(com)(\/\d*\/)(.*)$ https://example.com/ 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/ redirect;
# redirect category -> tag
rewrite (category\/)(.*)$ https://example.com/tag/ 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;
}
在 URL https://example.com/content/images/2020/01/logo.png
和 https://example.com/1886/slug
中,rewrite
指令看到的 URI 分别是 /content/images/2020/01/logo.png
和 /1886/slug
。
您需要重写第一个路径元素中包含 1 到 4 位数字的 URI。
使用其中之一:
rewrite ^/\d+(/.*)$ redirect;
或:
rewrite "^/\d{1,4}(/.*)$" redirect;
最后一个变体必须使用引号来保护嵌入的大括号字符。
详情见this document。