V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
vincent9
V2EX  ›  问与答

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

  •  
  •   vincent9 · 2021-03-25 10:34:58 +08:00 · 843 次点击
    这是一个创建于 1121 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我的框架是 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 伪静态规则,怎么写?

    2 条回复    2021-03-25 15:21:59 +08:00
    ryd994
        1
    ryd994  
       2021-03-25 13:18:14 +08:00 via Android
    location /app_1/ {
    try_files $uri $uri/ /app_1/index.php/$args;
    }
    以此类推

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

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

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

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

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

    }

    非常感谢 ryd994
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2514 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 01:11 · PVG 09:11 · LAX 18:11 · JFK 21:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.