title: '【网站】简易的nginx伪静态配置'
date: 2020-08-23 21:27:21
tags: [网站,水,懒,Tech,nginx]
published: true
hideInList: false
feature: https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1361281847,3390724955&fm=26&gp=0.jpg
isTop: false
只是想简单的弄弄,快被网上的教程弄懵了?看看这个!
前言
今天弄长链接longlonglink时,查了很多教程"^"、"d+"、"/"让我分不清啥是啥,由于时间不多,不能慢慢的系统性学习,知道看到了某项目的nginx配置,掌握了最简单的方法!
方法
当我们只是想单纯的将“example.com/index.php?id=1”换成"example.com/1"时可以试试以下的代码:
location / {
if (!-e $request_filename) {
rewrite ^(.*)/(.*)$ $1/index.php?&id=$2 last;
}
}
可以看到,其中“rewrite”里原链接以"^"开头"$"结尾,替换后链接为“$”开头"last"结尾。
“(.*)”即为"/"前或后的字符串。
比如“http://blog.bgsnd.com/a/a”其实就是 " (.)/(.)/(.*)"的格式,出现的第一个区块为"http://blog.bgsnd.com",在替换后的链接可以用$1代替,第二个区块为为“a”可以用$2代替,以此类推。。。
对上面的代码稍稍改动就可以实现你的伪静态了。
举个栗子⭐️
想把 "blog.bgsnd.com/post/2020/08/23/1.html"代替"blog.bgsnd.com/post/index.php?yr=2020&mo=08&da=23&id=1"的伪静态配置即为
location / {
if (!-e $request_filename) {
rewrite ^(.*)/post/(.*)/(.*)/(.*)/(.*).html$ $1/post/index.php?&yr=$2&mo=$3&da=$4&id=$5 last;
}
}
你GET了吗😁
Apache
认认真真的看完了教程发现自己服务器用的是Apache?没关系,试试这个转换器,将nginx的配置转换为Apache的配置。网址:https://winginx.com/en/htaccess
看不懂的干货