这样的 Nginx 上的伪静态规则怎么写

2021-03-25 10:34:58 +08:00
 vincent9

我的框架是 CodeIgniter.

默认的 URL 是这样 example.com/index.php/news/article/my_article

CodeIgniter 官方提供一条 Nginx 规则,去掉 index.php ,URL 可以变成这样 example.com/news/article/my_article

规则如下

location / {

try_files $uri $uri/ /index.php/$args;

}

我的情况这样的:

一个域名下,example.com ,通过目录建多个 CodeIgniter 项目,项目只在网站根目录下的一级子目录下建,不需考虑多级子目录建

各 CodeIgniter 的项目原始 URL 是这样

主项目 example.com/index.php/news/article/my_article

子项目 1 example.com/app_1/index.php/news/article/my_article

子项目 2 example.com/app_2/index.php/news/article/my_article

我想去掉主项目,各子项目的 URL 中的 index.php, 这样的 Nginx 伪静态规则,怎么写?

955 次点击
所在节点    问与答
2 条回复
ryd994
2021-03-25 13:18:14 +08:00
location /app_1/ {
try_files $uri $uri/ /app_1/index.php/$args;
}
以此类推

也可以用 regex capture 。但是你量不大的话这样更方便
vincent9
2021-03-25 15:21:59 +08:00
@ryd994

上面的方法非常好用,非常感谢!

我把他改成了正则,省的每次新增子项目,都去修改规则

location ~* /([0-9a-z]+)/ {

try_files $uri $uri/ /$1/index.php/$args;

}

非常感谢 ryd994

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/764902

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX