Home  >  Q&A  >  body text

301 redirect - nginx second-level domain name redirects to the directory

is the second-level domain name redirected to the directory. Because the second-level domain name is abandoned, it needs to be re-301.

For example, jianfei.xxx.com redirects to www.xxx.com/jianfei

In addition, www needs to be excluded. How to write it?

世界只因有你世界只因有你2713 days ago655

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-05-16 17:19:35

    if ($host = 'jianfei.xxx.com' ) {
         rewrite ^/(.*)$ http://www.xxx.com/jianfei/ permanent;
    }

    ------------------Change it--------
    The questioner said that jianfei is a variable, so the above method will not work.
    Consider whether you can create two configuration files
    www.xxx.com.conf, no need to rewrite

    server {
        listen       80;
        server_name  www.xxx.cn xxx.cn;
        

    other.xxx.com.conf, specially used to rewrite to www domain name

    server {
        listen       80;
        server_name  ~^(.*).xxx.com$;
        set $sub_name ;
        #if ($sub_name ~* (jianfei|gongjingmilan|others) ){#如果是部分子域名要跳转可以枚举或正则匹配,全部子域名都跳可以注释掉
            rewrite ^/(.*)$ http://www.xxx.com/$sub_name/;
        #}

    The test works

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-16 17:19:35

    Thank you very much Daniel, this method is better and solved

    reply
    0
  • Cancelreply