Skip to content

搞英语 → 看世界

翻译英文优质信息和名人推特

Menu
  • 首页
  • 作者列表
  • 独立博客
  • 专业媒体
  • 名人推特
  • 邮件列表
  • 关于本站
Menu

使用 Nginx 创建永久和临时重定向

Posted on 2024-01-01

作为将我的博客文章从/slug-of-post移动到/blog/slug-of-post更改的一部分,我需要在我的 Nginx 配置中设置永久重定向,这样旧的帖子链接就不会出现 404错误。Forge有一个 UI为此,但它只允许一次执行一个重定向,而我有大约 130 个要做。因此,我获取了所有帖子的列表,并在 Sublime Text [1]中使用多行编辑对其进行格式化。

将以下内容添加到您的 Nginx 配置中以对特定 URL 进行重定向。就我而言,它位于/etc/nginx/sites-available/rknight.me 。

 server {
# 301 Moved Permanently
rewrite ^/slug-of-post /blog/slug-of-post permanent ;

# 302 Found/Moved Temporarily
rewrite ^/slug-of-post /blog/slug-of-post redirect ;

# the rest of your nginx config here
}

您也可以对整个域执行此操作。虽然我不需要这样做,但这里也值得注意。

 server {
# redirect the root of the domain
rewrite ^/$ http://new.example.com permanent ;

# preserve paths
rewrite ^/(.*)$ http://new.example.com/ $1 permanent ;

# the rest of your nginx config here
}

  1. 我使用 VSCode 来做大多数事情,但 Sublime 仍然是处理多行编辑方面最好的⤾

原文: https://rknight.me/blog/creating-permanent-and-temporary-redirects-with-nginx/

本站文章系自动翻译,站长会周期检查,如果有不当内容,请点此留言,非常感谢。
  • Abhinav
  • Abigail Pain
  • Adam Fortuna
  • Alberto Gallego
  • Alex Wlchan
  • Answer.AI
  • Arne Bahlo
  • Ben Carlson
  • Ben Kuhn
  • Bert Hubert
  • Bits about Money
  • Brian Krebs
  • ByteByteGo
  • Chip Huyen
  • Chips and Cheese
  • Christopher Butler
  • Colin Percival
  • Cool Infographics
  • Dan Sinker
  • David Walsh
  • Dmitry Dolzhenko
  • Dustin Curtis
  • Elad Gil
  • Ellie Huxtable
  • Ethan Marcotte
  • Exponential View
  • FAIL Blog
  • Founder Weekly
  • Geoffrey Huntley
  • Geoffrey Litt
  • Greg Mankiw
  • Henrique Dias
  • Hypercritical
  • IEEE Spectrum
  • Investment Talk
  • Jaz
  • Jeff Geerling
  • Jonas Hietala
  • Josh Comeau
  • Lenny Rachitsky
  • Liz Danzico
  • Lou Plummer
  • Luke Wroblewski
  • Matt Baer
  • Matt Stoller
  • Matthias Endler
  • Mert Bulan
  • Mostly metrics
  • News Letter
  • NextDraft
  • Non_Interactive
  • Not Boring
  • One Useful Thing
  • Phil Eaton
  • Product Market Fit
  • Readwise
  • ReedyBear
  • Robert Heaton
  • Ruben Schade
  • Sage Economics
  • Sam Altman
  • Sam Rose
  • selfh.st
  • Shtetl-Optimized
  • Simon schreibt
  • Slashdot
  • Small Good Things
  • Taylor Troesh
  • Telegram Blog
  • The Macro Compass
  • The Pomp Letter
  • thesephist
  • Thinking Deep & Wide
  • Tim Kellogg
  • Understanding AI
  • 英文媒体
  • 英文推特
  • 英文独立博客
©2025 搞英语 → 看世界 | Design: Newspaperly WordPress Theme