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

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

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:

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

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

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

Passing proxy before and after rewrite rules:

  1. location /content/ {
  2. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  3. proxy_set_header X-Forwarded-Proto $scheme;
  4. proxy_set_header X-Real-IP $remote_addr;
  5. proxy_set_header Host $http_host;
  6. proxy_pass example_ip ;
  7. }

Full config:

  1. server {
  2. listen 80;
  3. listen [::]:80;
  4. server_name www.example.com example.com;
  5. if ($host = www.example.com) {
  6. return 301 https://$host$request_uri;
  7. } # managed by Certbot
  8. if ($host = example.com) {
  9. return 301 https://$host$request_uri;
  10. } # managed by Certbot
  11. }
  12. server {
  13. listen 443 ssl http2;
  14. listen [::]:443 ssl http2;
  15. # Redirect /id/slug -> slug
  16. rewrite ^(.*)(\/\d*\/)(.*)$ https://example.com/$3 redirect;
  17. # Redirect category -> tag
  18. rewrite (category\/)(.*)$ https://example.com/tag/$2 permanent;
  19. # Redirect blog -> archive
  20. rewrite (blog\/)$ https://example.com/archive permanent;
  21. root /var/www/ghost/system/nginx-root; # Used for acme.sh SSL verification (https://acme.sh)
  22. ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
  23. ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
  24. include /etc/nginx/snippets/ssl-params.conf;
  25. location / {
  26. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  27. proxy_set_header X-Forwarded-Proto $scheme;
  28. proxy_set_header X-Real-IP $remote_addr;
  29. proxy_set_header Host $http_host;
  30. proxy_pass http://example_ip;
  31. }
  32. location ~ /.well-known {
  33. allow all;
  34. }
  35. client_max_body_size 50m;
  36. }
英文:

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 />

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

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

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

passing proxy before and after rewrite rules<br />

  1. location /content/ {
  2. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  3. proxy_set_header X-Forwarded-Proto $scheme;
  4. proxy_set_header X-Real-IP $remote_addr;
  5. proxy_set_header Host $http_host;
  6. proxy_pass example_ip ;
  7. }

full config:

  1. server {
  2. listen 80;
  3. listen [::]:80;
  4. server_name www.example.com example.com;
  5. if ($host = www.example.com) {
  6. return 301 https://$host$request_uri;
  7. } # managed by Certbot
  8. if ($host = example.com) {
  9. return 301 https://$host$request_uri;
  10. } # managed by Certbot
  11. }
  12. server {
  13. listen 443 ssl http2;
  14. listen [::]:443 ssl http2;
  15. # redirect /id/slug -&gt; slug
  16. rewrite ^(.*)(\/\d*\/)(.*)$ https://example.com/$3 redirect;
  17. # redirect category -&gt; tag
  18. rewrite (category\/)(.*)$ https://example.com/tag/$2 permanent;
  19. # redirect blog -&gt; archive
  20. rewrite (blog\/)$ https://example.com/archive permanent;
  21. root /var/www/ghost/system/nginx-root; # Used for acme.sh SSL verification (https://acme.sh)
  22. ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
  23. ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
  24. include /etc/nginx/snippets/ssl-params.conf;
  25. location / {
  26. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  27. proxy_set_header X-Forwarded-Proto $scheme;
  28. proxy_set_header X-Real-IP $remote_addr;
  29. proxy_set_header Host $http_host;
  30. proxy_pass http://example_ip;
  31. }
  32. location ~ /.well-known {
  33. allow all;
  34. }
  35. client_max_body_size 50m;
  36. }

答案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:

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

Or:

  1. 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:

确定