S
S
SylarHero2014-01-27 13:34:40
Nginx
SylarHero, 2014-01-27 13:34:40

How to implement a redirect from a subdomain to another domain in Nginx?

In nginx config I tried using code like this.
server {
server_name ~^sub\.site\.ru$;
return 301 site2.ru/$request_uri;
}
I wanted the 'sub' subdomain to be redirected to another site (domain)
But I can't figure out why any subdomain is redirecting.
I need the redirect to work only with sub.site.ru Please help me
, where can there be an error.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2014-01-27
@Nc_Soft

server {
    listen 80;
    server_name sub.site.ru;
    return 301 $scheme://site2.ru$request_uri;
}

S
SylarHero, 2014-01-27
@SylarHero

I decided so, in the location ~ \.php$ section at the very beginning I added the following lines
if ($host ~ ^(sub\.site1\.ru)$ ) {
return 301 http://site2.ru$request_uri;
}
So even better, I just needed to do a redirect if it's php.
For example, when you go to the address sub.site1.ru/script.php, you will be redirected to site2.ru/script.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question