vincent9
V2EX  ›  问与答

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

  •  
  •   vincent9 · Mar 25, 2021 · 1607 views
    This topic created in 1874 days ago, the information mentioned may be changed or developed.

    我的框架是 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 replies    2021-03-25 15:21:59 +08:00
    ryd994
        1
    ryd994  
       Mar 25, 2021 via Android
    location /app_1/ {
    try_files $uri $uri/ /app_1/index.php/$args;
    }
    以此类推

    也可以用 regex capture 。但是你量不大的话这样更方便
    vincent9
        2
    vincent9  
    OP
       Mar 25, 2021
    @ryd994

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

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

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

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

    }

    非常感谢 ryd994
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   938 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 21:22 · PVG 05:22 · LAX 14:22 · JFK 17:22
    ♥ Do have faith in what you're doing.