大网数据广告活动
服务器运维问答

Nginx Rewrite subfolder to a sub domain and keep everything after it

· 52CE 我爱测

I am trying to rewrite the url to handle the following scenario, I want to redirect a few specific subfolders to a few specific subdomains and keep whatever was after the sub folder. So far I have: location / { rewrite ^/sub1/?(.…

问题原文

原帖链接 · Greyhammer · CC BY-SA 3.0

I am trying to rewrite the url to handle the following scenario, I want to redirect a few specific subfolders to a few specific subdomains and keep whatever was after the sub folder.

So far I have:

location / {
    rewrite ^/sub1/?(.*)$ https://sub1.example.com/$1 permanent;
    rewrite ^/sub2/?(.*)$ https://sub2.example.com/$1 permanent;
    rewrite ^/sub3/?(.*)$ https://sub3.example.com/$1 permanent;
    rewrite ^/sub4/?(.*)$ https://sub4.example.com/$1 permanent;
}

This handles example.com/sub1 example.com/sub1/ example.com/sub1/foobar
But not example.com/sub1/page.php this goes to example.com/sub1/page.php and 404's cause it doesn't exist.

I want
example.com/sub1 to go to sub1.example.com
example.com/sub1/ to go to sub1.example.com/
example.com/sub1/foobar to go to sub1.example.com/foobar
example.com/sub1/foo/bar/ to go to sub1.example.com/foo/bar/
example.com/sub1/page.php to go to sub1.example.com/page.php

What am I missing here? Is my regex that rusty?

EDIT: Here is my entire config for that site, in case its an issue with something else overriding it. https://pastebin.com/sEka1Q0e

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 3.0

In one of your server blocks, you have a location ~ \.php block alongside the location block containing the rewrite statements. That regular expressionlocationblock takes precedence when evaluating URIs which end with.php`.

One option is to move the rewrite statements into the server block, in which case they will be executed before any location block chosen. For example:

server {
    ...

    rewrite ^/sub1/?(.*)$ https://sub1.example.com/$1 permanent;
    rewrite ^/sub2/?(.*)$ https://sub2.example.com/$1 permanent;
    rewrite ^/sub3/?(.*)$ https://sub3.example.com/$1 permanent;
    rewrite ^/sub4/?(.*)$ https://sub4.example.com/$1 permanent;

    location / {
    }

    location ~ \.php$ 
    { 
        ... 
    }
    ...
}

Alternatively, a more efficient solution would be to place the rewrite statements inside individual prefix locations, but use the ^~ modifier to give them a higher precedence. See this document for details.

location ^~ /sub1 {
    rewrite ^/sub1/?(.*)$ https://sub1.example.com/$1 permanent;
}
location ^~ /sub2 {
    rewrite ^/sub2/?(.*)$ https://sub2.example.com/$1 permanent;
}
location ^~ /sub3 {
    rewrite ^/sub3/?(.*)$ https://sub3.example.com/$1 permanent;
}
location ^~ /sub4 {
    rewrite ^/sub4/?(.*)$ https://sub4.example.com/$1 permanent;
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-11-10)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。