简体   繁体   English

Nginx重写域和URL

[英]Nginx rewrite domain and URLs

I'm configuring nginx with multiple server names, and trying to set up the following rewrite rules 我正在使用多个服务器名称配置nginx,并尝试设置以下重写规则

redirect / on old.domain.com to new.domain.com/specific_page.php
redirect old.domain.com/$1 to new.domain.com/$1

In my "server" configuration, I have already the first rewrite condition working, but I cannot find the way to write the second. 在我的“服务器”配置中,我已经有第一个重写条件起作用,但是我找不到写第二个条件的方法。

if ($host = 'old.domain.com' ) {
    rewrite  ^/  http://new.domain.com/my-specific/link/list/info.php  permanent;
    rewrite  ^/(.*)$  http://old.domain.com/$request_uri?  permanent;
}

Any ideas how to handle easily this scenario? 任何想法如何轻松处理这种情况? (I realise this might be an unusual setup.) (我意识到这可能是不寻常的设置。)

Actually I managed to solve my problem : 实际上,我设法解决了我的问题:

if ($host = 'old.domain.com' ) {
    rewrite  ^/$  http://new.domain.com/my-specific/link/list/info  permanent;
    rewrite  ^(.*)$  http://old.domain.com$request_uri?  permanent;
}

the first rewrite rule ^/$ matches only http://old.domain.com/ and rewrites it to the requested URL 第一个重写规则^ / $仅匹配http://old.domain.com/并将其重写为请求的URL
The second rewrite rule ^(.*)$ matches whatever is behind the http://old.domain.com/ and rewrites the domain only. 第二个重写规则^(。*)$匹配http://old.domain.com/后面的内容,并且仅重写域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM