nginx 设置 WordPress 自定义固定链接后的地址转发 – 热爱改变生活
我的GitHub GitHub |     登录
  • If you can't fly, then run; if you can't run, then walk; if you can't walk, then crawl
  • but whatever you do, you have to keep moving forward。
  • “你骗得了我有什么用,这是你自己的人生”
  • 曾有伤心之地,入梦如听 此歌

nginx 设置 WordPress 自定义固定链接后的地址转发

服务器相关 sinvader 31143℃ 0评论

在使用 nginx 之前,为了实现伪静态,我在 wordpress 后台设置里面的固定链接中,将 “常用设置” 设置为了固定链接,并在里面默认值(/archives/%post_id%)的基础上在后面添加了 “.html”

后来使用了 nginx 之后,突然又发现.html 不能正常打开了,显示的是 nginx 的 404,没多想着急忙慌的就在固定链接里面选择了 “数字型”,倒是没问题了

可是我之前的外链怎么办啊?

以前在其他地方留文章的外链留下的可都是带 html 的,总不能放弃这些文章吧

于是就在 nginx 里面配置了一下转发规则,将符合规则的.html 路径对应到数字型的路径下,就可以正常访问了。

下面是两个我配置过的访问规则,我自己试了试,是可以的:

  1. if (ParseError: KaTeX parse error: Can't use function '\.' in math mode at position 38: …)/archives/(.*)\̲.̲html){
  2. rewrite (/archives/.*)\.html $1 permanent;
  3. }

这一条,适合于后缀为”.html” 的转成去掉 html 的,如 “https://sumile.cn/archives/1688.html” 转成 “https://sumile.cn/archives/1688”

  1. if (requestfilename (.)/archives/[09]request_filename ~ (.*)/archives/[0-9]*){
  2. rewrite (/archives/[0-9]*))1.html permanent;
  3. }

而这一条,适合给原来的链接添加后缀 html 的,如 “https://sumile.cn/archives/1688” 转成 “https://sumile.cn/archives/1688.html”

对于以上两条,如果你的 url 中不是如我一样的 “/archives/” 的话,那么将这个修改为你自己的部分

另外,你要转成什么样子的,就在 固定链接 里面的常用设置那里选择什么样子的:如我最后要转成”https://sumile.cn/archives/1688.html”,那么我在这里就应该选择 自定义链接,然后里面要填写 “/archives/%post_id%.html”

。如果我要转成”https://sumile.cn/archives/1688”,那么我选择 “数字型” 或者在 “自定义链接” 里面填写 “/archives/%post_id%” 就可以了

各位根据自己的需要自己修改就好了。

下面我再发一个我完整的配置

  1. server {
  2. listen 80;
  3. server_name sumile.cn www.sumile.cn ;
  4. autoindex off;
  5. index index.php;
  6.  
  7. location / {
  8. root C:\wamp\www\blog;
  9. index index.php;
  10. #if (ParseError: KaTeX parse error: Can't use function '\.' in math mode at position 38: …)/archives/(.*)\̲.̲html){
  11. # rewrite (/archives/.*)\.html 1permanent;1 permanent;
  12. ParseError: KaTeX parse error: Expected 'EOF', got '#' at position 8: #̲}
  13. if( if (request_filename ~ (.*)/archives/[0-9]*ParseError: KaTeX parse error: Expected '}', got 'EOF' at end of input: ){
  14. rewrite(/archives/[09] rewrite (/archives/[0-9]*) 1.htmlpermanent;1.html permanent;
  15. ParseError: KaTeX parse error: Expected 'EOF', got '}' at position 8: }̲
  16. if(f if (-frequest_filename/index.php){
  17. rewrite (.*) 1/index.php;1/index.php;
  18. ParseError: KaTeX parse error: Expected 'EOF', got '}' at position 9: }̲
  19. if(!f if (!-frequest_filename){
  20. rewrite (.*) /index.php;
  21. }
  22. if (-f ParseError: KaTeX parse error: Expected '}', got 'EOF' at end of input: …me/index.html){
  23. rewrite(.) rewrite (.*)1/index.html break;
  24. }
  25. }
  26. location ~ \.phpParseError: KaTeX parse error: Expected '}', got 'EOF' at end of input: {
  27. ParseError: KaTeX parse error: Undefined control sequence: \wamp at position 24: …ot C:\̲w̲a̲m̲p̲\www\blog;
  28. fastcgipass127.0.0.1:9000; fastcgi_pass 127.0.0.1:9000;
  29. fastcgiindexindex.php; fastcgi_index index.php;
  30. fastcgiparamSCRIPTFILENAME fastcgi_param SCRIPT_FILENAMEdocument_rootfastcgiscriptname;fastcgi_script_name;
  31. includefastcgiparams; include fastcgi_params;
  32. proxysetheaderHost proxy_set_header Hosthost;
  33. proxy_set_header X-Real-IP remoteaddr;remote_addr;
  34. proxysetheaderREMOTEHOST proxy_set_header REMOTE-HOSTremote_addr;
  35. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  36. }
  37. }
¥ 有帮助么?打赏一下~

转载请注明:热爱改变生活.cn » nginx 设置 WordPress 自定义固定链接后的地址转发


本博客只要没有注明“转”,那么均为原创。 转载请注明链接:sumile.cn » nginx 设置 WordPress 自定义固定链接后的地址转发

喜欢 (2)
发表我的评论
取消评论
表情

如需邮件形式接收回复,请注册登录

Hi,你需要填写昵称和邮箱~

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址