S
S
Stanislav Ezersky2016-06-03 09:28:19
Nginx
Stanislav Ezersky, 2016-06-03 09:28:19

Redirect from www to non-www?

Greetings!
I broke my brain how to redirect to a domain “from www” to “without www”.
A-records are spelled (@ and www), the domain is delegated to Yandex

server {
    listen      000.000.000.00:80;
    server_name mydomain.ru www.mydomain.ru;
    root        /home/user/web/mydomain.ru/public_html;
    index       index.php index.html;
..................

I tried to put a section at the beginning of the site config
server {
    server_name "~^(www\.)?(.*)$" ;
    return 301 http://$2$request_uri ;
}

null result
Tried like this
server {
    server_name  www.mydomain.ru;
    rewrite ^(.*) http://mydomain.ru$1 permanent;
}

server {
    listen      000.000.000.00:80;
    server_name mydomain.ru;
    root        /home/user/web/mydomain.ru/public_html;
    index       index.php index.html;

so
server {
    server_name  www.mydomain.ru;
    return 301 $scheme://mydomain.ru$request_uri;
}

server {
    listen      000.000.000.00:80;
    server_name mydomain.ru;
    root        /home/user/web/mydomain.ru/public_html;
    index       index.php index.html;

server {
...
   listen      000.000.000.00:80;
    server_name mydomain.ru www.mydomain.ru;
    return 301 $scheme://mydomain.ru$request_uri;
...
}

Wrong redirect on the page
How to solve the problem?
Debian 7 x64, NGINX+FPM

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Max, 2016-06-03
@EzS

server {
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

S
Stanislav Ezersky, 2016-06-03
@EzS

Problem in Yandex forwarding

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question