我可以根据 url 重定向不同的主目录吗?

can I redirect different home directories according to urls?

我在不同的目录中有 2 个 codeignitor 项目。

我想做的是

www.url.com => /var/www/ci_project
www.url.com/page1 => /var/www/ci_project

www.url.com/en => /home/another/ci_project
www.url.com/en/page1 => /home/another/ci_project

也就是说,只有当“en/”跟在主机后面时,我们才会使用不同的 ci 项目。

但是,apache 别名似乎无济于事,因为 ci 机制会覆盖路径。

我可以通过 apache2 或 nginx 实现吗?

对于 nginx,这样的东西应该可以工作:

index index.php;

location / {
    root /var/www/ci_project;
    try_files $uri $uri/ /index.php$is_args$args;
    location ~ \.php$ {
        # PHP-FPM handler here
    }
}

# without the following location, request to 'www.url.com/en'
# would be redirected with HTTP 301 code to 'www.url.com/en/'
location = /en {
    rewrite ^ /en/ last;
}

location /en/ {
    # remove the '/en' URI prefix.
    rewrite ^/en(/.*)  break;
    root /home/another/ci_project;
    try_files $uri $uri/ /en/index.php$is_args$args;
    location ~ \.php$ {
        rewrite ^/en(/.*)  break;
        # PHP-FPM handler here
    }
}

注意两个嵌套的 PHP 处理程序位置,它们是必需的,因为它们每个都应该使用自己的根。

我不熟悉 codeignitor,如果它依赖于 REQUEST_URI FastCGI 参数来确定请求的路由,那不会被 rewrite nginx 指令改变,你需要手动去除 /en URI 前缀(检查 答案的第一部分)。这是一个如何完成的示例:

map $request_uri $fixed_uri {
    ~^/en(/.*)$    ;
}

server {
    ...
    location /en/ {
        # remove the '/en' URI prefix.
        rewrite ^/en(/.*)  break;
        root /home/another/ci_project;
        try_files $uri $uri/ /en/index.php$is_args$args;
        location ~ \.php$ {
            rewrite ^/en(/.*)  break;
            # PHP-FPM handler here
            ...
            # this line should be AFTER the default fastcgi parameters file inclusion
            fastcgi_param REQUEST_URI $fixed_uri;
            ...
        }
    }
    ...
}

This 官方页面也可用于检查一些示例。