英文:
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:
- Don't rewrite for img:
https://example.com/content/images/2020/01/logo.png
- 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
- don't rewrite for img:
https://example.com/content/images/2020/01/logo.png
- 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 -> 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;
}
答案1
得分: 1
在URLs中,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+(/.*)$ $1 redirect;
或者:
rewrite "^/\d{1,4}(/.*)$" $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 "^/\d{1,4}(/.*)$" $1 redirect;
The last variant must use the quotes to protect the embedded brace characters.
See this document for details.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论