D
D
Den Alex Smith2019-01-24 11:46:46
Apache HTTP Server
Den Alex Smith, 2019-01-24 11:46:46

301 redirect from WWW to non-WWW in site.ru.conf how to implement?

There is a main site site.ru , which is now for some reason redirected to www.site.ru in the config (see below).
There are subdomains sub1, sub2, ... , subN (for which, as I understand it, such a ServerAlias ​​is registered) TASK
: You need to set up a 301 redirect from WWW to without WWW Directory
structure for the main site:
/SOME_PATH/site/site.ru / www
Directory structure for subdomains:
/SOME_PATH/site/site.ru/ sub1
/SOME_PATH/site/site.ru/ sub2
...
/SOME_PATH/site/site.ru/ subN
Actually, config:

<VirtualHost 127.0.0.1:8080>
        ServerName   site.ru
        ServerAlias  *.site.ru
        ServerSignature Off
        RewriteEngine on
        RewriteCond %{HTTP_HOST} ^site\.ru [NC]
        RewriteRule ^/(.*) http://www.site.ru/$1 [L,R]

        ErrorDocument 404 /?error=404
        ServerAdmin  "[email protected]"
        ServerAdmin  "[email protected]"
        AssignUserID site site
        DocumentRoot /SOME_PATH/site/site.ru/www
        VirtualDocumentRoot /SOME_PATH/site/site.ru/%1
        CustomLog /var/log/apache2/site.ru.access.log combined
        ErrorLog /var/log/apache2/site.ru.error.log
        ServerSignature On
        Options FollowSymLinks Includes MultiViews ExecCGI
        UseCanonicalName Off
        <IfModule mod_ssl.c>
                SSLEngine off
        </IfModule>
        <Directory /SOME_PATH/site/site.ru/www>
        Options +ExecCGI
        AllowOverride All
        <IfModule sapi_apache2.c>
                php_admin_flag engine on
                php_admin_flag safe_mode off
                php_admin_value open_basedir "/SOME_PATH/site/site.ru/www/:/tmp"
        </IfModule>
        <IfModule mod_php5.c>
                php_admin_flag engine on
                php_admin_flag safe_mode off
                php_admin_value memory_limit 128M
                php_admin_value upload_max_filesize 128M
                php_admin_value post_max_size 32M
                php_admin_value open_basedir "/SOME_PATH/site/site.ru/www:/tmp"
        </IfModule>
        <IfModule mod_fcgid.c>
                <Files ~ (\.fcgi)>
                        SetHandler fcgid-script
                        Options +FollowSymLinks +ExecCGI
                </Files>
        </IfModule>
        </Directory>
 </VirtualHost>

It did not help (gives Error 404) an expression like:
RewriteCond %{HTTP_HOST} ^www\.site\.ru$ [NC]
RewriteRule ^(.*)$ http://site.ru/$1 [R=301,L]

UPD: In addition, it also turned out that nginx is on top
netstat -nltp | grep 80
tcp        0      0 127.0.0.1:8080          0.0.0.0:*               LISTEN      7079/apache2
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7007/nginx

Config:
server {
        listen 80;
        server_name site.ru www.site.ru;
        root /SOME_PATH/site/site.ru/www;
        access_log /var/log/nginx/site.ru.access.log;
        error_log /var/log/nginx/site.ru.error.log info;

                error_page 404 = @fallback;
                location / {
                        proxy_pass http://127.0.0.1:8080;
                        proxy_set_header Host $host;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_set_header X-Real-IP $remote_addr;
                }
                location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
                        access_log /dev/null;
                        error_log /dev/null crit;
                }
                location @fallback {
                        proxy_pass http://127.0.0.1:8080;
                        proxy_set_header Host $host;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_set_header X-Real-IP $remote_addr;
                }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dodo512, 2019-01-24
@netcrox

It will be more convenient to have separate VirtualHost for site.ru, www.site.ru , *.site.ru.

<VirtualHost 127.0.0.1:8080>
        ServerName   www.site.ru
        Redirect /  http://site.ru/
</VirtualHost>

<VirtualHost 127.0.0.1:8080>
        ServerName   site.ru
        DocumentRoot /SOME_PATH/site/site.ru/www

        ...

</VirtualHost>


<VirtualHost 127.0.0.1:8080>
        ServerName   site.ru
        ServerAlias  *.site.ru
        
        VirtualDocumentRoot /SOME_PATH/site/site.ru/%1
        
        ...

D
Den Alex Smith, 2019-01-25
@netcrox

WORKING SOLUTION
As a result, I understood what everything was connected with. Insidious nginx. On a tip , dodo512 did some digging and got the following:

<VirtualHost 127.0.0.1:8080>
        ServerName  site.ru
        ServerSignature Off
        RewriteEngine on
        ErrorDocument 404 /?error=404
        ServerAdmin  "[email protected]"
        ServerAdmin  "[email protected]"
        AssignUserID site site
        DocumentRoot /SOME_PATH/site/site.ru/www
        CustomLog /var/log/apache2/site.ru.access.log combined
        ErrorLog /var/log/apache2/site.ru.error.log
        ServerSignature On
        Options FollowSymLinks Includes MultiViews ExecCGI
        UseCanonicalName Off
        <IfModule mod_ssl.c>
                SSLEngine off
        </IfModule>
        <Directory /SOME_PATH/site/site.ru/www>
        Options +ExecCGI
        AllowOverride All
        <IfModule sapi_apache2.c>
                php_admin_flag engine on
                php_admin_flag safe_mode off
                php_admin_value open_basedir "/SOME_PATH/site/site.ru/www/:/tmp"
        </IfModule>
        <IfModule mod_php5.c>
                php_admin_flag engine on
                php_admin_flag safe_mode off
                php_admin_value memory_limit 128M
                php_admin_value upload_max_filesize 128M
                php_admin_value post_max_size 32M
                php_admin_value open_basedir "/SOME_PATH/site/site.ru/www:/tmp"
        </IfModule>
        <IfModule mod_fcgid.c>
                <Files ~ (\.fcgi)>
                        SetHandler fcgid-script
                        Options +FollowSymLinks +ExecCGI
                </Files>
        </IfModule>
        </Directory>
</VirtualHost>

<VirtualHost 127.0.0.1:8080>
        ServerName   www.site.ru
        ServerAlias  *.site.ru
        ServerSignature Off
        RewriteEngine on

    ErrorDocument 404 /?error=404
        ServerAdmin  "[email protected]"
        ServerAdmin  "[email protected]"
        AssignUserID site site
        DocumentRoot /SOME_PATH/site/site.ru/www
        VirtualDocumentRoot /SOME_PATH/site/site.ru/%1
        CustomLog /var/log/apache2/site.ru.access.log combined
        ErrorLog /var/log/apache2/site.ru.error.log
        ServerSignature On
        Options FollowSymLinks Includes MultiViews ExecCGI
        UseCanonicalName Off
        <IfModule mod_ssl.c>
                SSLEngine off
        </IfModule>
        <Directory /SOME_PATH/site/site.ru/www>
        Options +ExecCGI
        AllowOverride All
        <IfModule sapi_apache2.c>
                php_admin_flag engine on
                php_admin_flag safe_mode off
                php_admin_value open_basedir "/SOME_PATH/site/site.ru/www/:/tmp"
        </IfModule>
        <IfModule mod_php5.c>
                php_admin_flag engine on
                php_admin_flag safe_mode off
                php_admin_value memory_limit 128M
                php_admin_value upload_max_filesize 128M
                php_admin_value post_max_size 32M
                php_admin_value open_basedir "/SOME_PATH/site/site.ru/www:/tmp"
        </IfModule>
        <IfModule mod_fcgid.c>
                <Files ~ (\.fcgi)>
                        SetHandler fcgid-script
                        Options +FollowSymLinks +ExecCGI
                </Files>
        </IfModule>
        </Directory>
</VirtualHost>

server {
    listen 80;
    server_name www.site.ru;
    return 301 http://site.ru$request_uri;
}
server {
        listen 80;

        server_name site.ru;
        root /SOME_PATH/site/site.ru/www;

        access_log /var/log/nginx/site.ru.access.log;
        error_log /var/log/nginx/site.ru.error.log info;

                error_page 404 = @fallback;
                location / {
                        proxy_pass http://127.0.0.1:8080;
                        proxy_set_header Host $host;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_set_header X-Real-IP $remote_addr;
                }
                location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
                        access_log /dev/null;
                        error_log /dev/null crit;
                }
                location @fallback {
                        proxy_pass http://127.0.0.1:8080;
                        proxy_set_header Host $host;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_set_header X-Real-IP $remote_addr;
                }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question